Skip to main content
New Participant
March 10, 2017
Answered

If then Statements for a Javascript in PDF form

  • March 10, 2017
  • 1 reply
  • 30669 views

Hellos,

I have created a form that I need to implement an If Then statement. Basically if the answer to one field is a "Product" then I need a particular field to return a Value  as 10.

Basically If Field A is ProductA  Field B=10
If Field A is ProductB then Field B =15
If Field A is ProductC then Field B = 20
Is there a way that this can be incorporated as a formula?

Thank you

Varghese J

This topic has been closed for replies.
Correct answer try67

Sure. You can use this code as the custom calculation script for "Field B":

var fieldA = this.getField("Field A").valueAsString;

if (fieldA=="ProductA") event.value = 10;

else if (fieldA=="ProductB") event.value = 15;

else if (fieldA=="ProductC") event.value = 20;

else event.value = "";

1 reply

try67
try67Correct answer
Adobe Expert
March 10, 2017

Sure. You can use this code as the custom calculation script for "Field B":

var fieldA = this.getField("Field A").valueAsString;

if (fieldA=="ProductA") event.value = 10;

else if (fieldA=="ProductB") event.value = 15;

else if (fieldA=="ProductC") event.value = 20;

else event.value = "";

Participating Frequently
February 7, 2020

So I have this custom calc to auto populate phone numbers if a certain insurance is selected... but my insurance field is set to allow custom text for third party insurances. How do I add a code to allow for custom text if a custom text is put in the insurance section? 

 

var fieldA = this.getField("Insurance").valueAsString;
if (fieldA=="BCBS Federal") event.value = ("1(800)442-4607");
else if (fieldA=="BCBS TX") event.value = ("1(800)451-0287");
else if (fieldA=="BCBS") event.value = ("1(800)676-2583");
else if (fieldA=="UHC") event.value = ("1(877)842-3210");
else if (fieldA=="Cigna") event.value = ("1(800)244-6224");
else event.value="";

 

Thom Parker
Adobe Expert
February 7, 2020

The problem with making this field available for manual entry as it is now, is that it's fighting the calculation. Since the value of this field is set from a single source, it is a simple matter to move the script to that source, which means there is no conflict for manual entry. This is your best solution. 

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