11 Replies Latest reply on Mar 19, 2013 1:57 PM by JohnT416

# Custom Calculation Script for If, If Else

I have a field named Freq01 where three values can be input; 1, 2, or 3 or it can be left blank. I have another field, Cost01, where I want one of three values to appear, depending upon what number is input in Freq01. Seemed to be a simple enough function!!! I'm using an old version of Acrobat Pro, 6.0.2, Mac OS X, 10.3.9.

In Excel the function input in cell D11 is:

=IF((D11=1),3200,(IF(D11=2,3000,(IF(D11=3,2700,0)))))

And I think the JavaScript code would be:

I've been trying to figure out how to write it in this AcroJS and I can't get it!!!

I've tried various combinations of the following, having == instead of = , this.getField("Freq01").value instead of Freq01.value , event.value instead of FullPrice.value , having " " or not...

if (Freq01.value = "1")
{
FullPrice.value = "3200"
}
else if
(Freq01.value = "2")
{
FullPrice.value = "3000"
}
else if
(Freq01.value = "3")
{
FullPrice.value = "2700"
}

Thanks!!!

rd
• ###### 1. Re: Custom Calculation Script for If, If Else
First of all, in order to compare two values you MUST use the "==" operator. Using only a single equals sign will assign the value on the right to the variable on left and will return a 1. So that has to change.

In addition, what you're looking for is probably the "switch" command (known sometimes as select case in other languages).

switch (Freq01.value) {
case "1": // Do some stuff
break;
case "2": // Do some other stuff
break;
case "3": // ...
break;
case else: // In case it is none of the above do this
}

Don't forget to add the "break;" command or all the subsequent commands will be executed.

Edit: just remembered. If the value in question is not a string but an integer, drop the quotes around "1", "2", "3", etc.
• ###### 2. Re: Custom Calculation Script for If, If Else
JavaScript has a problem with numbrers and text as numbers, so it is a good practice to force numbers to a string with the ".valueAsString" property or ".toString()" method or to a number. Since the values are very distinct, you do not not need the "else" just a series of "if" would do. Also you did not provide for the no match case:

FullPrice.value = ''; // clear result field
if (Freq01.valueAsString == "1")
{
FullPrice.value = "3200";
}
if
(Freq01.valueAsString = "2")
{
FullPrice.value = "3000";
}
if
(Freq01.value.toString() = "3")
{
FullPrice.value = "2700";
}

Or with the "switch()" statement:

switch (Freq01.valueAsString) {
case "1":
FullPrice.value = "3000" ;
break;
case "2":
FullPrice.value = "3000"
break;
case "3": // ...
FullPrice.value = "2700";
break;
default:
FullPrice.value = "";
break;
} // end switch Freq01 value

}
• ###### 3. Re: Custom Calculation Script for If, If Else
Well, I tried all of the above and each time the only output that appears for my FullPrice field is still just what ever value I have assigned when Freq01 is 1. Where is this going wrong???
• ###### 4. Re: Custom Calculation Script for If, If Else
rollsdownhill, if you used Geo's code, then it won't work. He used a single "=" for comparisons, which is wrong. You must use "==". Please post your code if you want more help.
• ###### 5. Re: Custom Calculation Script for If, If Else
Try the following code as the custom calculation script of your FullPrice field:
```// Get the Freq01 field value
var v1 = getField("Freq01").valueAsString;

// Set up a prices object to associate field value with price
var prices = {
"1": 3200,
"2": 3000,
"3": 2700
};

// Set default value for this field
var v2 = "";

// Get price from prices object and assign it to this field's value
if (typeof prices[v1] != "undefined") {
v2 = prices[v1];
}

event.value = v2;

```

George
• ###### 6. Re: Custom Calculation Script for If, If Else
Thanks George, this last one worked ^_^ !!!

And try67, I did try using *==* instead of *=* in Geo's code and I still couldn't get it working correctly... Also tried various combos of the following; value or valueAsString and having or not having " " around the numerical values. And Acrobat didn't like *case else:* told me there was a syntax error on that line.

Could it have been my version? It is quite old, 2003, Acrobat 6.0.2 for Mac.

Anyway, got a working code.

Thanks again!!!
• ###### 7. Re: Custom Calculation Script for If, If Else
Sorry, replace "case else" with "default". My bad.
• ###### 8. Re: Custom Calculation Script for If, If Else
I should not have cut and pasted.

var Freq01 = this.getField('Freq01').value;
event.value = '';
if (Freq01 == 1)
event.value = 3200;

if(Freq01 == 2)
event.value = 3000;

if(Freq01 == 3)
event.value = 2700;

switch(this.getField('Freq01').value.toString()) {
case '1':
event.value = 3200;
break;

case '2':
event.value = 3000;
break;

case '3':
event.value = 2700;
break;

default:
event.value = '';
break;
}

I have substituted the "event.value" for the field name and placed the script in the "Custom calculation script" box of the "Full Price" field, so the script more closely resembles Excel.
• ###### 9. Re: Custom Calculation Script for If, If Else

Are you still there?  If so, how whould you edit this to allow for a different price list in a different state?

If State=1

then use price list A

If State=2

then use price list B

• ###### 10. Re: Custom Calculation Script for If, If Else

Pretty open ended question.

How does your form work or how are items selected?

How many products are there?

Does each product have unique price that is not duplicated by another product?

How many states will there be?

How often to you expect to update the product prices?

Will you be adding new items and how often?

Will sales tax and shipping cost be computed or included in prices?

• ###### 11. Re: Custom Calculation Script for If, If Else

Thanks for the quick response.  I have managed to solve the problem myself.

There are 2 states with 12 products each.  Variable to each is selected by combo box.  Product list is fairly stable.  Prices may be updated quarterly.

Not being a seasoned programmer I may have left a hole or two.  Here is the code with a partial product list.

// set variable for State

var v0 = getField("ComboBox8").valueAsString;

//set variable for product

var v1 = getField("ComboBox6").valueAsString;

if (v0=="GA") {

// Set up a "Charges" object to associate field value with Charge

var charge = {

"Bi-W \$150": 15.22,

"Bi-W \$250": 27.84,

"Bi-W \$300": 32.88

};

}

Else

// state <>”GA”

var charge = {

"Bi-W \$150": 13.74,

"Bi-W \$250": 25.68,

"Bi-W \$300": 30.72

};

// Set default value for this field

var v2 = "";

// Get charge from charges object and assign it to this field's value

if (typeof charge[v1] != "undefined") {

v2 = charge[v1];

}

1. event.value = v2;