These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Javascript equation question...what needs to be changed to make this script work?

smolloy
Registered: Oct 21 2011
Posts: 2

I have a simple calculation that i need to write as a JS in adobe Acrobat 9 professional. There are two varianbles. One is from a radial button (selected only if a condition exists otherwise it remains unselected). If the button is selected the export value is "yes". The other variable is user input (a quantity). When the quantity is input one of five things will happen.
 
1. if the quantity is >= 50 and <100 and the other variable is not selected, then the result is the quantity times 25.
2. if the quantity is >=100 and the other variable is not selected, then the result is the quantity times 21.70.
3. if the quantity is >= 50 and <100 and the other variable is selected, then the result is the quantity times 25 + 80.
4. if the quantity is >=100 and the other variable is not selected, then the result is the quantity times 21.70 + 80.
5. there is a separate script that prevents a quantity less than 50 from being selected.
 
Below is the script i created. i'm new to java so please let me know what to fix
 
event.value = 0
var setup = this.getField(“yes”).exportvalue + var quantity = this.getField("ValetTrayQty").value;
if(quantity >= 50 && quantity < 100 & setup = null)
event.value = quantity * 25.00;
else if (quantity >= 100; && setup = null)
event.value = quantity * 21.70;
if(quantity >= 50 && quantity < 100 & setup = yes)
event.value = quantity * 25.00 + 80;
else if (quantity >= 100; && setup = yes)
event.value = quantity * 21.70 + 80;
 

My Product Information:
Acrobat Pro 9.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
If the quantity is really guaranteed to be 50 or greater, than this can be simplified to the following:

  1. // Get the field values
  2. var setup = getField(“yes”).value;
  3. var quantity = +getField("ValetTrayQty").value;
  4.  
  5. // Initialize result value
  6. var result = quantity * 25;
  7.  
  8. if (quantity >= 100) {
  9. result = quantity * 21.7;
  10. }
  11.  
  12. // Add 80 if radio button is selected
  13. if (setup !== "Off") result += 80;
  14.  
  15. // Set this field value
  16. event.value = result;
Note that the value of a check box or radio button group is "Off" when none are selected, not null.
smolloy
Registered: Oct 21 2011
Posts: 2
Hi George, thanks for the quick reply. This is my first time using JS (you're probably thinking no kidding). I see the efficiency in the code that you've constructed.

A few things are still out of whack. I'm getting "illegal character 2: at line 3" in the script below.

Also, I believe my attempt at correcting (below) is wrong. The getField for "yes" was incorrect, I mis-informed you, sorry about that. The value is set to yes in the Export Value for the Check box. So the export value is from a check box named CustomLogo. I'm assuming Acrobat assigns a value ("yes" as the Export Value) if the Check Box is selected. So i'm assuming the getField for var setup is not correct. Any advice?


// Get the field values
var setup = getField(“EventValue”).value;
var quantity = +getField(“ValetTrayQty”).value;
// Initialize result value
var result = quantity * 25;
if (quantity >= 100) {
result = quantity * 21.7;
}
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Yes, the quote characters are wrong for the second line. Since you said the correct field name is "CustomLogo", the second line should be:

var setup = getField("CustomLogo").value;


With the code I posted you don't need to know what the export value is, since you know what the value is if the field is not selected.