Skip to main content
Known Participant
April 29, 2019
Answered

order form sum total - can I shorten this code?

  • April 29, 2019
  • 2 replies
  • 1263 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

Legend
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'?!

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
April 29, 2019

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 = "";

try67
Community Expert
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.