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

Scripting a nested if/then statement

kencodes
Registered: Jan 21 2008
Posts: 1
Answered

I'm also new to scripting and would like to convert the following nested statement to a FormCalc or Java script. I'm sure it is possible but I'm out of my element.

TIA

cc is a selection from a dropdown

if(cc="ud",(uap*1.16)+4.95,(if(cc="multi",(uap*1.16)+13.50,(if(cc="ivpb",(uap*1.16)+67,(if(cc="TPN",(uap*1.16)+250,(if(cc="pom",4.95,0.0)))))))))

The result displays as currency.

Ken

My Product Information:
LiveCycle Designer, Windows
radzmar
Expert
Registered: Nov 3 2008
Posts: 1202
There are several ways to write a calculation script for this task.
I think a switch case function (JavaScript) will be the best for it.
Put it into the change:event of the dropdown box.

  1. var uap = NumericField123.rawValue; //Define the value of the variable 'uap'
  2. switch (xfa.event.newText)
  3. {
  4. case "ud":
  5. uap * 1.16 + 4.95
  6. break;
  7. case "multi":
  8. uap * 1.16 +13.50
  9. break;
  10. case "ivpb":
  11. uap * 1.16 + 67
  12. break;
  13. }

radzmar
LoveCycle Blog
Documents you need:
LiveCycle Designer ES2 Docs

JCASE
Registered: May 12 2010
Posts: 29
Radzmar - I use that soultion frequently and it works great. Two questions/scenarios though.

1. What if that dropdown box is in a dynamic subform and lets say you delete the subform. How does the uap recalculate?

2. What if, instead of a calculation script, the switch case controls an instanceManager script. So if you pick a text in the dropdown box, it adds an instance, but if you switch to a new text, how do you delete the prior instance?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You could write a function and pass the values as parameters and the function would return the computed value. That way the entire decision and result is field independent.

function SelectValue(cType, nUap) {
//Define the value of the variable 'uap'
.var nTotal = NumericField123.rawValue;
// on cType compute value
switch (cType) {
case "ud":
nTotal = uap * 1.16 + 4.95;
break;
case "multi":
nTotal = uap * 1.16 +13.50;
break;
case "ivpb":
nTotal = uap * 1.16 + 67;
break;
// define action for all other values
default:
nTotal = 0;
} // end switch
// return computed value
return nTotal;
} // end function


// JavaScript calculation
$.rawValue = SelectValue(xfa.event.newText, NumericField123.rawValue);

George Kaiser