Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Custom JavaScript Code for If/Then/Else Statement

New Here ,
May 26, 2010 May 26, 2010

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!

36.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
LEGEND ,
May 26, 2010 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.

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 26, 2010 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 26, 2010 May 26, 2010

Outstanding! Worked perfect the first time!

Thank you SO much for your help!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 15, 2013 Jun 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 15, 2013 Jun 15, 2013

You'll need to provide some more information about how the price should be

calculated.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 17, 2013 Jun 17, 2013

let's say if you have 1-50 sq feet the pricewill be calculated with  10 $/ sq feet. if you have 50-100 feet the price will be calculated with 9 $/ sq feet....and so on until the margin of the range ( 7$ is reached). In fact i need to be able to calculatethe price of the sq feet based of the amount of total sq  feet

Untitled.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 17, 2013 Jun 17, 2013

So as the custom calculation code for the Price field you can use something like this:

var totalSqFt = Number(this.getField("TotalSqFt").value);

if (totalSqFt<50) event.value = 10;

else if (totalSqFt<100) event.value = 9;

else if (totalSqFt<200) event.value = 8; //etc.

else event.value = 7; // minimum price

Then the Total Price is just Total Sq Ft * Price. You'll just need to make sure that Total Price is calculated after the Price field, to avoid problems.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 17, 2013 Jun 17, 2013

i understand but is something that is not working...because if we consider that the totalsqft is 40 ...let's say, than,  this means that is smaller than 50 and also smaller than 100 ...in the end,  with which value will be done the calculation?...with 10 or with 9?....also the code is not working...i don't know why, i am absolutely null at javascript so i need a code exactly how must be on my form...sorry for boedering you but if i knew how to do i will never  boeder you guys....that's way i must thank in advance.

So if you can help me with a code just to copy paste i will be more than gratful.

in other words the code must be construct, i think, based on the expression.... if...7<totalsqft<10....but i repeat myself...i don't know javascript i just think logicaly

thank you !

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 17, 2013 Jun 17, 2013

That's why I used the if-else if-else construct... It basically does this:

- Is the value smaller than 50? If so, price is 10.

- If not, is the value smaller than 100 (ie bigger than or equals to 50 and smaller than 100)? If so, price is 9.

- If not, is the value smaller then 200 (ie bigger than or equals to 100 and smaller than 200)? If so, price is 8;

- If not (ie bigger or equals to 200), price is 7.

If it's not working check the JS console for errors.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 17, 2013 Jun 17, 2013

i can't figure how to do...can i send the form  to you?....maybe you will be able to write the corect code.

thank you again!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 17, 2013 Jun 17, 2013

Yeah, you can send it to try6767@gmail.com.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 17, 2013 Jun 17, 2013

thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 17, 2013 Jun 17, 2013

Very few programming languages let you test an expression like 7 < X < 10.

But most of them let you write it as two tests  7 < X      and also   X < 10

How you write the "and also" depends on the progrmaming language. In JavaScrip you can write

( 7 < X  ) && ( X < 10 )

Notice it is two ampersands &&. One ampersand is accepted but doesn't do what you want.

For more complex cases if ... else if ... else if ... is useful. Notice the else, the crucial thing which means it is only applicable if other tests fail.

Please don't ask for code to copy/paste. It's helpful to know you are new to JavaScript, but that doesn't mean you shouldn't be trying to learn... We are here to help you learn how to do something, not to do it for you! (In my opinion).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 17, 2013 Jun 17, 2013

i understand perfect your oppinion regarding the process of learning and i am agree with you . Sincerely speaking i don't know where to find a turorial which to tell me from zero (begginer) to a user with some knoledge like this kind that i need in this case. I appreciate any help and one again i am agree with you.

thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 17, 2013 Jul 17, 2013

I need some help as well. I am trying to input an if statement that says.

If the Fieldx is greater than 45, then input 45, otherwise, input the number in Fieldx.

I am not good with javascript so I could use some assistance please.

Thank you

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 17, 2013 Jul 17, 2013
LATEST

There are many ways to use custom JavaScript calculations to perform this task. With custom calculations one needs to know the name of the fields to process.

var nValue = Number(this.getField("Name of field to test").value); // get the value of the field to test as a number;

// test the value of the field

if(nValue > 45) {

// value is greater than 45 - set field value to 45;

event.value = 45;

} else {

// value not greater than 45 set field value to tested field value;

event.value = nValue;

} // end of test

Or one could test for the minimum of the 2 values

var nValue = Number(this.getField("Name of field to test").value); // get the value of the field to test as a number;

// set value to the minimum of 45 of the value of the tested field

event.value = Math.min(45, nValue);

// end of calculation;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines