10 Replies Latest reply on Feb 8, 2018 9:18 AM by Thom Parker

Acrobat X Pro calculation problem

I have an order form which has six items for sale. The first field on each item is the number of items the customer is purchasing. The second field is a hidden field which creates the cost of the item. The third field is simply first field + second field = total cost for that item.  There are another five lines of exactly the same code. The problem comes in with the final Grand Total line. I wasn't able to simply perform a sum function and pick my fields. I had to do a custom calculation.  It works. However, if the person picks something from line one and skips to line three, the grand total turns into a mess.

Examples:

Field One (how many, limited to three characters - a/k/a "order.0.0")

Field Two (hidden - validate value of 75 to 75, or whatever each line costs - a/k/a "cost.0.0")

Field Three (custom calculation script):

var a = this.getField("order.0.0")

if(a.value!=0)

event.value = a.value * 75

else

event.value = ""

Each line is assigned a different variable, and the "a.value" changes to whatever the publication costs. Customer needed the if/else statement to keep the zeroes out of the way if nothing was ordered.

The final "grand total" calculation is done this way:

var a = this.getField("price.0.0");

var b = this.getField("price.1.0");

var c = this.getField("price.2.0");

var d = this.getField("price.3.0");

var e = this.getField("price.4.0");

var f = this.getField("price.5.0");

event.value = a.value + b.value + c.value + d.value + e.value + f.value;

All works fine as long as no fields are skipped. Otherwise, \$75.00 + \$120.00 ends up being \$75,120.00 instead of the actual \$195.00. I am not a programmer; is there something else I am missing? Thank you!

• 1. Re: Acrobat X Pro calculation problem

There are a few different ways to get your calculation correct. One method, which I think is the correct way is to allow all the fields to contain real number values, so all the calculations flow nicely with none of these empty field issues. I know you want the 0 value fields to show blank, so to do this use a format script to control the field display.

However, the easiest thing for you to do right now is to fix the calculation so that it tests for blank fields and doesn't add them in.

Here's the script, I noticed you use group naming, so this script will work as long as there are no "price.0.1, price.1.1, etct" fields.

var sum = 0;

this.getField("price").getArray().forEach(function(a){sum+=a.valueAsString.length?a.value: 0;});

event.value = sum;

• 2. Re: Acrobat X Pro calculation problem

You need to explicitly convert all the values to numbers. Use this code:

var a = Number(this.getField("price.0.0").valueAsString);

var b = Number(this.getField("price.1.0").valueAsString);

var c = Number(this.getField("price.2.0").valueAsString);

var d = Number(this.getField("price.3.0").valueAsString);

var e = Number(this.getField("price.4.0").valueAsString);

var f = Number(this.getField("price.5.0").valueAsString);

event = a + b + c + d + e + f;

• 3. Re: Acrobat X Pro calculation problem

That works too

Here's a shorter modified script. With this script you can add any number of order lines and the sum script stays the same because it relies on the group naming.

var sum = 0;

this.getField("price").getArray().forEach(function(a){sum+=Number(a.value);});

event.value = sum;

• 4. Re: Acrobat X Pro calculation problem

This code assumes that those are all of the "price" fields. There might be more, which are not involved in this calculation...

• 5. Re: Acrobat X Pro calculation problem

I mentioned that in my first post, but the easiest and most extensible solution is name the fields so that they are properly grouped.

• 6. Re: Acrobat X Pro calculation problem

var sum = 0;

this.getField("price").getArray().forEach(function(a){sum+=Number(a.value);});

event.value = sum;

This does work and adds things properly. Is there any way to modify it so that the final total doesn't show the \$0.00 in the field before it adds items up?  Some of our users still cling to hand-writing these forms.

• 7. Re: Acrobat X Pro calculation problem

Change the last line to:

event.value = sum==0 ? "" : sum;