Skip to main content
Participant
July 27, 2025
Answered

Using Conditional IF with a Checkbox to calculate in Another Field

  • July 27, 2025
  • 1 reply
  • 223 views

I'm stumped but then again new to this.  I would be grateful for any thoughts:
*. VC (A check Box)
*. VendorComp  (formatted as a Number)
*. TotalTaxDue   (formatted as a Number)
When the box is checked, the calculation should be:
VendorComp = TotalTaxDue * 1.5/100 // 1.5% of TotalTax Due)
When unchecked:
VendorComp = 0
I've added the following Custom Calculation in VendorComp:
---------------------------------------------------------------------------------------
var f = this.getField("VC").value; // If Checked, then "Yes", If unChecked or Blank then NOT EQUAL to "Yes"

if (f="Yes") // Means the box is checked
{
this.getField("VendorComp").value=Math.round(this.getField("TaxTotalDue").value*1.5/100);
}

else (f!="Yes") // Means the box is Unchecked
{
this.getField("VendorComp").value=Math.round(this.getField("TaxTotalDue").value*0/100);
}

--------------------------------------------------------------------------
It does not work.  If I remove the conditional and ONLY have the formula:

this.getField("VendorComp").value=Math.round(this.getField("TaxTotalDue").value*1.5/100)
That works.  So I must assume I'm not doing the conditional IF correctly.
I have tried "Switch" as well to no success.  However, I may be doing that incorrectly as well.

Any help would very much appreciated.

Thanks.

Louis

Correct answer Thom Parker

There are a few issues withthis code. The most visible is a syntax error on the "else" statement.  Look in Acrobat JavaScript Console Window (Ctrl-J) and you will see it reported. But the initial comparison in the "if" is incorrect.  The equality operator is "==".   Also, the result of a calculation must be placed in "event.value". And there are other issues. 

  

Here's a simplification, properly formated, version of your code that fixes the problems:

// Custom Calculation script of the "VendorComp" field
if (this.getField("VC").value != "Off"){
   event.value = Math.round(this.getField("TaxTotalDue").value*0.015);
}
else{
    event.value = 0;
}

 

You can read about writing calculation scripts here:

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

 

1 reply

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
July 27, 2025

There are a few issues withthis code. The most visible is a syntax error on the "else" statement.  Look in Acrobat JavaScript Console Window (Ctrl-J) and you will see it reported. But the initial comparison in the "if" is incorrect.  The equality operator is "==".   Also, the result of a calculation must be placed in "event.value". And there are other issues. 

  

Here's a simplification, properly formated, version of your code that fixes the problems:

// Custom Calculation script of the "VendorComp" field
if (this.getField("VC").value != "Off"){
   event.value = Math.round(this.getField("TaxTotalDue").value*0.015);
}
else{
    event.value = 0;
}

 

You can read about writing calculation scripts here:

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
July 27, 2025

Thom:
How can I thank you.  I learned quite a lot and this was very helpful.  AWESOME!  Thank you so much.

Thom Parker
Community Expert
Community Expert
July 27, 2025

You are welcome 🙂

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often