Skip to main content
m.raby
Participating Frequently
February 13, 2019
Question

Calculation script for fields with text

  • February 13, 2019
  • 1 reply
  • 1137 views

I have a form I am making that I have 3 fields that default value is N/A

I am trying to make a 4th field to add these 3 fields but the problem is if the field is not displaying a value then it the calculation script does not work. It only works if all 3 fields have a numerical value.

I have to have the field to display N/A if there is not a numerical value in it.

So my thoughts have been for the 4th field to convert the N/A value to 0 but how do I use the value if it is not N/A. I think I am over complicating it in my head and that is why I can't come up with a solution.

My original script:

var nDownPayment = this.getField("DEAL_PURCHASE_TRADE_TOTALNET1").value + this.getField("DEAL_PURCHASE_CASHDOWN").value + this.getField("DEAL_PURCHASE_REBATE").value;

if( nDownPayment > 0.00) event.value = this.getField("DEAL_PURCHASE_TRADE_TOTALNET1").value + this.getField("DEAL_PURCHASE_CASHDOWN").value  + this.getField("DEAL_PURCHASE_REBATE").value;

else event.value = 0.00

but this script doesn't display a value if "DEAL_PURCHASE_TRADE_TOTALNET1" and "DEAL_PURCHASE_CASHDOWN" have the default value of N/A and "DEAL_PURCHASE_REBATE" has a value in it.

How do I get it to either ignore the first 2 fields if their value is equal to "N/A" or treat "N/A" as the value of 0 only for the purpose of the calculation script.

DEAL_PURCHASE_TRADE_TOTALNET1 & DEAL_PURCHASE_CASHDOWN & DEAL_PURCHASE_REBATE will always be either a numerical value or text "N/A"

Thanks in advance for any help in solving this.

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
February 13, 2019

Do not use "N/A" as the default value. Instead, make the default "0" and use a custom format script to display the N/A.

Custom format script:

if(event.value == 0)

    event.value = "N/A";

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
m.raby
m.rabyAuthor
Participating Frequently
February 13, 2019

That worked for displaying N/A but the calculation script still doesn't calculate unless all 3 fields have a numerical value in them even though I went into each and put the value of 0.00 and it displayed N/A correctly but still doesn't calculate.

I think I have gotten close with this script. But it is only calculating the last field DEAL_PURCHASE_REBATE and not calculating the other fields even if they all have a numerical value in them.

var nTotalNet = this.getField("DEAL_PURCHASE_TRADE_TOTALNET1").value;

var nCashDown = this.getField("DEAL_PURCHASE_CASHDOWN").value;

var nRebate = this.getField("DEAL_PURCHASE_REBATE").value;

if (nTotalNet == "N/A") {event.value = 0.00;}

else {event.value = nTotalNet;}

if (nCashDown == "N/A") {event.value = 0.00;}

else {event.value = nCashDown;}

if (nRebate == "N/A") {event.value = 0.00;}

else {event.value = nRebate;}

var nDownPayment = this.getField("nTotalNet").value + this.getField("nCashDown").value + this.getField("nRebate").value;

if( nDownPayment > 0.00) {event.value = nTotalNet + nCashDown + nRebate;}

else {event.value = 0.00;}

try67
Community Expert
Community Expert
February 13, 2019

Your logical conditions are overlapping, making only the last one have any effect on the value of the field.

You need to carefully think about how it is supposed to work and build the code based off of that.