Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.



Excerpt
hiddentrue

if(condition, trueExpression, falseExpression) Processed trueExpression if condition is true, falseExpression if condition is false.


Function: if()

Process an Expression (and return its value) depending on whether a condition evaluates to True or False.

...

Note that if you want to process more than a one-line expression if condition evaluates to True or False then wrap the script in a do() function. See the examples below.

Examples

Code Block
if( in.UserID == "Test", 0, in.numCalls)

Returns the value of in.numCalls unless in.UserID is equal to "Test".


Code Block
if( startsWith(in.telNo,"44"), do ( $priceBand = "NATIONAL", $rate = 0.03 ) )

This example shows how to use a do() function if you want to process several expressions (a script) based on the result of an if(). Note that the $-Variables $priceBand and $rate are only set if in.telNo starts with "44".

A common mistake is to omit the do() function in cases like the previous example, as shown below:

Code Block
if( startsWith(in.telNo,"44"), $priceBand = "NATIONAL", $rate = 0.03 )

With this expression, only $priceBand = "NATIONAL" is evaluated if the condition returns True, and only $rate = 0.03 is evaluated if the condition returns False.


Code Block
if( startsWith(in.telNo,"44"), 
   
   do( $priceBand = "NATIONAL", $rate = 0.03 )
      
      ,// Else 
   
   do( $priceBand = "INTERNATIONAL", $rate = 0.60 ) 

)

This example extends the example above to include an "else" clause i.e. the expressions that should be processed if the condition returns False.

Please note the layout in the last example. Consistent formating formatting of your scripts like this will help clarify where the "else" script starts. Also see Scripting Style.

...