These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

sum problems

chrisamp
Registered: Jan 21 2009
Posts: 11
Answered

Hi,

I'm getting frustrated with this..i'm sure it's simple but my custom calculation script doesnt work
It doesnt add properly.

var a = this.getField("c_CASH_Row 16_2").value;
var b = this.getField("c_CASH_Row 2_2").value;
var c = this.getField("c_CASH_Row_12_2").value;
var d = this.getField("c_CASH_Row_13_2").value;
var e = this.getField("c_CASH_Row_14_2").value;
var f = this.getField("c_CASH_Row_15_2").value;
var g = this.getField("c_CASH_Row_18_2").value;
var h = this.getField("c_CASH_Row_19_2").value;
var i = this.getField("c_CASH_Row_1_2").value;
var j = this.getField("c_CASH_Row_20_2").value;
var k = this.getField("c_CASH_Row_22_2").value;
var l = this.getField("c_CASH_Row_24_2").value;
var m = this.getField("c_CASH_Row_26_2").value;
var n = this.getField("c_CASH_Row_27_2").value;
var o = this.getField("c_CASH_Row_30_2").value;
var p = this.getField("c_CASH_Row_31_2").value;
var q = this.getField("c_CASH_Row_32_2").value;
var r = this.getField("c_CASH_Row_3_2").value;
var s = this.getField("c_CASH_Row_4_2").value;
var t = this.getField("c_CASH_Row_5_2").value;
var u = this.getField("c_CASH_Row_6_2").value;
var v = this.getField("c_CASH_Row_7_2").value;
var w = this.getField("c_CASH_Row_8_2").value;
var x = this.getField("c_CASH_Row_9_2").value;

var sum = a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r+s+t+u+v+w+x;

event.value = sum;

if (sum > 200000) {

// Set value of field to zero
event.value = 0;

app.alert({cMsg: "Your total funding request cannot exceed $200,000.00", nIcon: 1, nType: 0, cTitle: "error", });

}

Please help! Thank you!

My Product Information:
Acrobat Pro 8.0, Macintosh
chrisamp
Registered: Jan 21 2009
Posts: 11
this works...heh

(function () {

// Initialize variable to hold the sum of the fields
var sum = 0;

var a = this.getField("c_CASH_Row 16_2").value;
var b = this.getField("c_CASH_Row 2_2").value;
var c = this.getField("c_CASH_Row_12_2").value;
var d = this.getField("c_CASH_Row_13_2").value;
var e = this.getField("c_CASH_Row_14_2").value;
var f = this.getField("c_CASH_Row_15_2").value;
var g = this.getField("c_CASH_Row_18_2").value;
var h = this.getField("c_CASH_Row_19_2").value;
var i = this.getField("c_CASH_Row_1_2").value;
var j = this.getField("c_CASH_Row_20_2").value;
var k = this.getField("c_CASH_Row_22_2").value;
var l = this.getField("c_CASH_Row_24_2").value;
var m = this.getField("c_CASH_Row_26_2").value;
var n = this.getField("c_CASH_Row_27_2").value;
var o = this.getField("c_CASH_Row_30_2").value;
var p = this.getField("c_CASH_Row_31_2").value;
var q = this.getField("c_CASH_Row_32_2").value;
var r = this.getField("c_CASH_Row_3_2").value;
var s = this.getField("c_CASH_Row_4_2").value;
var t = this.getField("c_CASH_Row_5_2").value;
var u = this.getField("c_CASH_Row_6_2").value;
var v = this.getField("c_CASH_Row_7_2").value;
var w = this.getField("c_CASH_Row_8_2").value;
var x = this.getField("c_CASH_Row_9_2").value;


sum = a+b+c+d+e+f+g+h+i+j+k+l+m+n+o+p+q+r+s+t+u+v+w+x;

event.value = sum;

if (sum > 200000) {app.alert({cMsg: "Your total funding request cannot exceed $200,000.00", nIcon: 1, nType: 0, cTitle: "error", });

}

})();
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
Your first script does not constrict the field values to be summed to numeric values only. If one does not use this constriction, any blank field will be treated like a string and change the addition to concatenation.

You could also simplify your script by using a function to sum the numeric values from an array of field names:
function SumArray(aFields) {/* sum an Array of field namesParameter:

aFields = array of field names to sum

Returns sum of fields

*/
var sum = 0; // result of summation of fields// loop through the array of field namesfor(i = 0; i < aFields.length; i++) {// add value of i element from array of field namessum += Number(this.getField(aFields[i]).value);} // end of loopreturn sum; // return computed value // define an array of field names to sumvar aFieldNames = new Array("c_CASH_Row_1_2", "c_CASH_Row 2_2","c_CASH_Row_3_2", "c_CASH_Row_4_2", "c_CASH_Row_5_2","c_CASH_Row_6_2", "c_CASH_Row_7_2", "c_CASH_Row_8_2","c_CASH_Row_9_2", "c_CASH_Row_12_2", "c_CASH_Row_13_2","c_CASH_Row_14_2", "c_CASH_Row_15_2", "c_CASH_Row 16_2","c_CASH_Row_18_2", "c_CASH_Row_19_2", "c_CASH_Row_20_2","c_CASH_Row_22_2", "c_CASH_Row_24_2", "c_CASH_Row_26_2","c_CASH_Row_27_2", "c_CASH_Row_30_2", "c_CASH_Row_31_2","c_CASH_Row_32_2"); // sum the array of field namesevent.value = SumArray(aFieldNames);// test the resultif(event.value > 200000) {app.alert({cMsg: "Your total funding request cannot exceed $200,000.00", nIcon: 1, nType: 0, cTitle: "error", });event.value = ''; // clear field} // end test

And if you placed the function as document level JavaScript, you could reuse the code to sum any group of fields by just passing the function the array list of field names.

George Kaiser

chrisamp
Registered: Jan 21 2009
Posts: 11
very cool, thank you!