Skip to main content
Known Participant
April 29, 2019
Answered

order form sum total - can I shorten this code?

  • April 29, 2019
  • 2 replies
  • 1257 views

I know there are document-level advanced formulas, but that's way beyond my level. I have an order form that is working well, thanks to Adobe community help . Quantity x Price = Product Total. I am at the total dollar for all products field, and once again want the field blank rather than $0.00 to start. So I'm not using the basic 'select all the fields option'.

I'm about to put the following code in which, with 49 fields, will be quite lengthy. I am also applying the Number AsString coding offered for a previous issue, but don't know if that is the right use here. (I presume it is since I am adding numbers.) Is there a shorter way (without going to the document level coding), or do I just keep adding fields?

I did try it with 49 qty but the total was shifting the decimal place: $10.50 + $8.50 were added as $105.00 and $85.00. When I took it back down to test as just qty1 + qty2, it is working ok.

Thanks for any insights and assistance.

var qty1 = Number(this.getField("tikmb").valueAsString);

var qty2 = Number(this.getField("tdtb1").valueAsString);

if( (qty1 + qty2 ) >0) event.value = qty1 + qty2;

else event.value = "";

This topic has been closed for replies.
Correct answer try67

var total = 0;

for (var i=1; i<=49; i++) {

    var fname = "tp"+i;

    var f = this.getField(fname);

    if (f==null) console.println("ERROR: " + fname);

    else total+=Number(f.valueAsString);

}

if (total>0) event.value = total;

else event.value = "";

2 replies

Brainiac
April 29, 2019

Make sure there is no $ sign in the values, or you can't add them up.

Known Participant
April 29, 2019

ok, so I will rename all my product total fields as 'tp1' 'tp2' etc - then what's the 'simple loop'?!

Known Participant
April 29, 2019

I don't quite understand the first issue. I would need to see the actual file to find out what's wrong there.

Regarding the second issue, this part of the code is incorrect:

for (var i=1; i>=7 && i<=49; i++) {

Use this instead:

for (var i=7;  i<=49; i++) {


Thanks - I had tried it that way first but must have made a mistake elsewhere since it didn't work. The fix worked, and I just did a check of calculation order, and a manual check of all fields and calculations.

So the only 'error' is the merchandise tax code not being blank. Would you be willing to take a quick look? Obviously I appreciate it if you can't. If so, where do I send it?

Thanks once again for all your help.

try67
Community Expert
April 29, 2019

The best way would be to give you field consistent names, like Qty1, Qty2, Qty3, etc., and then you would be be able use a simple loop to add them all up.