Copy link to clipboard
Copied
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!
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;
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
This code assumes that those are all of the "price" fields. There might be more, which are not involved in this calculation...
Copy link to clipboard
Copied
I mentioned that in my first post, but the easiest and most extensible solution is name the fields so that they are properly grouped.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Change the last line to:
event.value = sum==0 ? "" : sum;
Copy link to clipboard
Copied
Isn't there a way to mark both your reply and Thom Parker's as correct? His initial script worked for the addition problem, and yours worked for the zero problem! You both need correct answer notifications!
Copy link to clipboard
Copied
Doesn't really matter (to me, at least). Mark his reply as correct and mine as helpful, if you wish.
Copy link to clipboard
Copied
Well thanks Gilad