Skip to main content
Known Participant
Apr 29, 2019 2:43 PM UTC
Answered

order form sum total - can I shorten this code?

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 is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Apr 29, 2019 3:09 PM UTC

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

9 replies

Legend
Apr 29, 2019 2:58 PM UTC

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

Translate
Known Participant
Apr 29, 2019 3:04 PM UTC

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

Translate
try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
Apr 29, 2019 3:09 PM UTC

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

Translate
try67
Community Expert
Community Expert
Apr 29, 2019 2:55 PM UTC

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.

Translate