Search code examples
javascriptpdffoxit

sum field values if they meet conditions


I am attempting to sum a number of field values but only if a condition associated with those filed values is met. For example:

I have a PDF form with several number value fields. Each of those number fields is accompanied by a choice in status. I simply want to add all of the number fields of the same status. Here is the code I tried and thought was working but it seems that if a status is changed on a Field previously added, it fails to work. For example, if I add Field1, Field2 and Field4 because they are the same status but I go back and change Field2's status to something different, it stops working:

// field names
var cTotal1 = "Day1Pd1Total";
var cTotal2 = "Day1Pd2Total";
var cTotal3 = "Day1Pd3Total";
var cTotal4 = "Day1Pd4Total";
var cTotal5 = "Day1Pd5Total";
var cTotal6 = "Day1Pd6Total";
var cStatus1 = "Day1Pd1Status";
var cStatus2 = "Day1Pd2Status";
var cStatus3 = "Day1Pd3Status";
var cStatus4 = "Day1Pd4Status";
var cStatus5 = "Day1Pd5Status";
var cStatus6 = "Day1Pd6Status";
// get conditonal field values
var vStatus1 = this.getField(cStatus1).value;
var vStatus2 = this.getField(cStatus2).value;
var vStatus3 = this.getField(cStatus3).value;
var vStatus4 = this.getField(cStatus4).value;
var vStatus5 = this.getField(cStatus5).value;
var vStatus6 = this.getField(cStatus6).value;
// get field values base on condition
if(vStatus1 == "1"){
var vTotal1 = this.getField(cTotal1).value;
}else{
var vTotal1 = "";
}
if(vStatus2 == "1"){
var vTotal2 = this.getField(cTotal2).value;
}else{
var vTotal2 = "";
}
if(vStatus3 == "1"){
var vTotal3 = this.getField(cTotal3).value;
}else{
var vTotal3 = "";
}
if(vStatus4 == "1"){
var vTotal4 = this.getField(cTotal4).value;
}else{
var vTotal4 = "";
}
if(vStatus5 == "1"){
var vTotal5 = this.getField(cTotal5).value;
}else{
var vTotal5 = "";
}
if(vStatus6 == "1"){
var vTotal6 = this.getField(cTotal6).value;
}else{
var vTotal6 = "";
} 
// clear result value
event.value = "";
// add values
var FLPTotal = vTotal1 + vTotal2 + vTotal3 + vTotal4 + vTotal5 + vTotal6;
event.value = FLPTotal;

How do I get this script to recalculate when there are changes? This is also in a PDF file.

Edit: Further testing shows that the running order appears to be the issue. vTotal1 will add successfully to vTotal2 and then to vTotal3 as long as they are populated in that order. vTotal1 will not sum with vTotal3. How do I sum the fields in any order?


Solution

  • First of all, I am not exactlly sure why you have created multiple variables for each field. Would something like this work?

    var FLPTotal = 0;
    

    Then for if one of the conditions is met you just add the value to it.

    FLPTotal += this.getField(cTotal3).value;
    

    If it returns as a string you could do.

    FLPTotal += parseInt(this.getField(cTotal3).value);
    

    The following would add up all the values of the cTotals.

    // field names
    var cTotal1 = "Day1Pd1Total";
    var cTotal2 = "Day1Pd2Total";
    var cTotal3 = "Day1Pd3Total";
    var cTotal4 = "Day1Pd4Total";
    var cTotal5 = "Day1Pd5Total";
    var cTotal6 = "Day1Pd6Total";
    var cStatus1 = "Day1Pd1Status";
    var cStatus2 = "Day1Pd2Status";
    var cStatus3 = "Day1Pd3Status";
    var cStatus4 = "Day1Pd4Status";
    var cStatus5 = "Day1Pd5Status";
    var cStatus6 = "Day1Pd6Status";
    // get conditional field values
    var vStatus1 = this.getField(cStatus1).value;
    var vStatus2 = this.getField(cStatus2).value;
    var vStatus3 = this.getField(cStatus3).value;
    var vStatus4 = this.getField(cStatus4).value;
    var vStatus5 = this.getField(cStatus5).value;
    var vStatus6 = this.getField(cStatus6).value;
    // Add total variable
    var FLPTotal = 0;
    // get field values base on condition
    if(vStatus1 == "1"){
    FLPTotal += parseInt(this.getField(cTotal1).value);
    }
    if(vStatus2 == "1"){
    FLPTotal += parseInt(this.getField(cTotal2).value);
    }
    if(vStatus3 == "1"){
    FLPTotal += parseInt(this.getField(cTotal3).value);
    }
    if(vStatus4 == "1"){
    FLPTotal += parseInt(this.getField(cTotal4).value);
    }
    if(vStatus5 == "1"){
    FLPTotal += parseInt(this.getField(cTotal5).value);
    }
    if(vStatus6 == "1"){
    FLPTotal += parseInt(this.getField(cTotal6).value);
    }
    // clear result value and set result value
    event.value = "";
    event.value = FLPTotal;
    

    Tell me if this works.