Skip to main content
Participant
May 26, 2010
Answered

Custom JavaScript Code for If/Then/Else Statement

  • May 26, 2010
  • 1 reply
  • 37377 views

Can someone help me figure out this code. I have a field in a PDF that I need to generate a discount based on original price of an item. If Price is greater than 5000 then I need the discount field to say 1500. Else the discount field needs to be Price* .30.

I tried this but it didn't work: If price>5000 then discount=1500 else discount=price*.3

Anyone have some help?

Thank you!

    This topic has been closed for replies.
    Correct answer gkaiseril

    Acrobat forms use JavaScript so you need to use the JavaScirpt syntax for the proper JavaScript syntax for the 'if...else' statement. You also need to access the field object as established by the Acrobat JavaScript Object by using the 'getField' method of the doc object, 'this.getField(cName)' and then the 'value' property for that object.

    // custom calculation script for the 'discount field

    // establish the name of the price field

    var cPrice = 'price';

    // get the value of the price field

    var nPrice = this.getField(cPrice).vallue;

    // assume the discount is 30% of price

    event.value = nPrice * 0.30;

    // set discount to 1,500 if price is greater than 5,000

    if(nPrice > 5000) {

    event.value = 1500;

    }

    // end of custom calculation script

    For more information see Conditional Execution by Thom Parker.

    1 reply

    gkaiserilCorrect answer
    Inspiring
    May 26, 2010

    Acrobat forms use JavaScript so you need to use the JavaScirpt syntax for the proper JavaScript syntax for the 'if...else' statement. You also need to access the field object as established by the Acrobat JavaScript Object by using the 'getField' method of the doc object, 'this.getField(cName)' and then the 'value' property for that object.

    // custom calculation script for the 'discount field

    // establish the name of the price field

    var cPrice = 'price';

    // get the value of the price field

    var nPrice = this.getField(cPrice).vallue;

    // assume the discount is 30% of price

    event.value = nPrice * 0.30;

    // set discount to 1,500 if price is greater than 5,000

    if(nPrice > 5000) {

    event.value = 1500;

    }

    // end of custom calculation script

    For more information see Conditional Execution by Thom Parker.

    CSScottAuthor
    Participant
    May 26, 2010

    Outstanding! Worked perfect the first time!

    Thank you SO much for your help!

    Participating Frequently
    June 15, 2013

    Hello Experts,

    for you i am sure that my problem is quite simple, but for me is a big one because i just starting to learn Javascript.Sothis is the problem:

    i have a PDF form in which i have some fields to calculate  sq feet for some windows; after the calculations are done, i need a code which must display a price per sq feet based on the amount of sq feet.

    The range for price depends of the sq feet amount must be between  7 and 10 $. This price must be generate in a field named " price per sq feet"

    Anybode can help me?

    Thank you all