7 Replies Latest reply on Jan 27, 2011 9:06 AM by gkaiseril

# calculating totals: if, else statements

OK, I have a form... with 7 fields called receipt1 through receipt7. I also have a total field called reimbursementTotal.

I need a script to add all the receipt fields up and if the total is less than or equal to \$150, give the reimbursementTotal field a value of the total, else give the reimbursementTotal a value of \$150.

I am not well versed in Adobe Scripting, some help would be appreciated.

Thanks,

wtm

• ###### 1. Re: calculating totals: if, else statements

Try the following as the custom calculation script for the reimbursementTotal text field:

// Custom calculation script

(function () {

// Calculate the total

var total = 0;

for (var i = 1, i < 8, i++) {

total += +getField("receipt" + i).value;

}

// Set this field value based on the total

event.value = (total > 150) ? 150 : total;

})();

• ###### 2. Re: calculating totals: if, else statements

SyntaxError: missing ; after for-looop initializer 6: at line 7

it highlights: total += +getField("receipt" + i).value;

• ###### 3. Re: calculating totals: if, else statements

You have to write a custom calculation script for the total.

// define an array of field name to sum

var aFields = new Array("FieldName0", "FieldName1", "FieldName2");

// sum the values for the array of field names

var nSum = 0; // variable for the sum

var nValue; // variable for a specific field value

// loop through each element of the field name array

for(i = 0; i < aFields.length; i++) {

nValue = this.getFeild( aFields[i] ).value; // get i element field value

// see if value of field is a null string

if( nValue.toString() == "") nValue = 0; // if null set to zero

nSum = Number(nSum) + Number(nValue); // add value to sum

} // end loop

// set field value to the minimum of the or 150

event.value = Math.min(nSum, 150);

• ###### 4. Re: calculating totals: if, else statements

Console pops up with the following errors...

this.getFeild is not a function

8:AcroForm:reimbursementTotal:Calculate

TypeError: this.getFeild is not a function

8:AcroForm:reimbursementTotal:Calculate

this.getFeild is not a function

8:AcroForm:reimbursementTotal:Calculate

TypeError: this.getFeild is not a function

8:AcroForm:reimbursementTotal:Calculate

• ###### 5. Re: calculating totals: if, else statements

Comma's were the issue on line 6...

Had to change code to the following

// Custom calculation script

(function () {

// Calculate the total

var total = 0;

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

total += +getField("receipt" + i).value;

}

// Set this field value based on the total

event.value = (total > 150) ? 150 : total;

})();

It worked!

Thanks so much,

wtm

• ###### 7. Re: calculating totals: if, else statements

The line:

nValue = this.getFeild( aFields[i] ).value; // get i element field value