Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Custom Calculation Script

Community Beginner ,
Dec 11, 2020 Dec 11, 2020

I'm having a difficult time figuring out a formula for my pdf. There are two different ones that I need.

 

1. If "Field A" is 1 then take it times $X.XX if "Field A" is > 1 then take it times $XX.XX

 

2. If "Field B" is 1 then take it times $price1; if "Field B" is 2 then take it times $price2; if "Field B" is 3 or greater then take it times $price3

 

Thanks for your help! 

TOPICS
PDF forms
693
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Dec 11, 2020 Dec 11, 2020

OK, that's very simple. Use something like this code as the custom calculation for "Total Field A":

 

var price1 = 10;
var price2 = 20;
var qty = Number(this.getField("Field A").valueAsString);
if (qty==0) event.value = 0;
else if (qty==1) event.value = price1;
else event.value = price2 * qty;

 

You can adjust that code for the B fields, too.

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 11, 2020 Dec 11, 2020

What do you mean by "take it", exactly? Are you talking about changing the same field's value (A or B), or another field?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 11, 2020 Dec 11, 2020

Here it is a little clearer. 

 

1. If "Field A" is 1 then "Field A" times $X.XX if "Field A" is > 1 then "Field A" times $XX.XX

 

2. If "Field B" is 1 then "Field B" times $price1; if "Field B" is 2 then "Field B" times $price2; if "Field B" is 3 or greater then "Field B" times $price3

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 11, 2020 Dec 11, 2020

That is problematic because you've basically described a circular calculation. Let's take scenario 1, for example. Let's say the first price is 10 and the second is 20, and the initial value of field A is 1.

So as per your description it now becomes 10. Next time the calculations are updated (which is each time any field in the file is edited!), the value of A is 10, so it is now 10 * 20 = 200. Next time it will be 200 * 20 = 4000, etc., etc..

Do you see the issue there? You have to separate the two things. One field should be used for the quantity, and another for the total price. You can't combine the two.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 11, 2020 Dec 11, 2020

This formula is in the "Total Field" at the end of the row. So the first row has a "Field A" where the customer puts in the quantity of product they want. If that number is 1 then "Field A" times $price1 and the answer is displayed in "Total Field A" at the end of the row. If the number entered in "Field A" is 2 or higher, then "Field A" times $price2 and that answer is displayed in "Total Field A" at the end of the row. 

 

Same way with the "Field B" row. There is a "Total Field B" at the end of that row. The formula is in that box and calculates depending on what number is entered into "Field B" box. Screen Shot 2020-12-11 at 10.21.55 AM.jpgexpand image

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 11, 2020 Dec 11, 2020

OK, that's very simple. Use something like this code as the custom calculation for "Total Field A":

 

var price1 = 10;
var price2 = 20;
var qty = Number(this.getField("Field A").valueAsString);
if (qty==0) event.value = 0;
else if (qty==1) event.value = price1;
else event.value = price2 * qty;

 

You can adjust that code for the B fields, too.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 14, 2020 Dec 14, 2020
LATEST

Thanks! You're a genius! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines