Need help getting a form field on Acrobat to do a conditional equation (if, then).

I am trying to set up a form so that it will compare the total credit against total debit and if credit is higher than debit, return the difference to one form field and if credit is lower than debit, return that difference to a different form field.


Anita Crum


2 Answers

Voted Best Answer

This can be done. If you have a consolidated calculation (put all calculations into one single script, attaching it to a field which is not even part of the calculation), you can add the following code (you will have to adjust the field names):


var cre = this.getField("inCredit") ; // credit greater than debit
var deb = this.getField("inDebit") ; // debit greater than credit

var totcredit = this.getField("totalCredit") ; // total credit
var totdebit = this.getField("totalDebit") ; // total debit

var diff = totcredit.value - totdebit.value ; // that's the difference
cre.value = "" ; // clear the fields
deb.value = "" ;
if (Math.abs(diff) > 0.00001) {
if (diff > 0) {
cre.value = diff ;
} else {
cre.value = diff ;
}
}

Note that neither field will show a value if the balance is 0; if you want another behavior for balance = 0, you would add an "else" path to the first comparison.

Because of the way numbers are represented and calculated in JavaScript, there may be a little difference, even if they are essentially the same, we consider any value smaller than 0.00001 to be 0.

And that should do it.

Hope this can help.

Max Wyss.


By Max Wyss   

Have you read this article?
http://acrobatusers.com/tutorials/con...


Thom Parker   


Please specify a reason: