NexJ Logo

Branching functions

Branching functions are used to create conditional expressions, where a different value is returned based on a set of conditions.

If()

Evaluates the specified expression and returns one value if it is true and another value if it is false. This function takes as its argument any expression that can be evaluated as true or false. It returns a specified value if the expression evaluates to true, or a different value if the expression evaluates to false. The returned value can be an expression. Returning an expression allows you to create branching expressions, where an If() function returns another expression that is subsequently evaluated.

Syntax

If(<expression_to_evaluate>, <return_if_true>, <return_if_false>)

Input

expression_to_evaluate

expression

The expression to evaluate to true or false.

return_if_true

Boolean, date, integer, number, string, expression

The value to return if the expression evaluates to true.

return_if_false

Boolean, date, integer, number, string, expression

The value to return if the expression evaluates to false.

Output

Boolean, date, integer, number, string, expression

Examples

Assume that you have a form in which there is a number question with the reference name customer_age. If the question is answered 45, the expression If($customer_age < 65, "Standard Rate", "Senior Discount") returns "Standard Rate" because the expression evaluates as true.

The previous expression can be modified to include an If() statement as one of its conditional return values. The expression If($customer_age < 12, "Child Discount", If($customer_age < 65, "Standard Rate", "Senior Discount")) returns "Standard Rate" because the first expression evaluates to false and the second expression evaluates to true.

IfNull()


Evaluates the specified expression and returns different values depending on whether the result is null or not null. This function takes any expression as an argument. It returns the expression's result if it is not null, or a specified value if it is null. The returned value can be an expression. Returning an expression allows you to create branching expressions, where an IfNull() function returns another expression that is subsequently evaluated.

Syntax

IfNull(<expression_to_evaluate>, <return_if_null>)

Input

expression_to_evaluate

expression

The expression to evaluate.

return_if_null

Boolean, date, integer, number, string, expression

The value to return if the expression's result is null.

Output

Boolean, date, integer, number, string, expression

Examples

Assume that you have a form in which there is a number question with the reference name number_of_cars. If the question is not answered, the expression IfNull($number_of_cars, 0) returns 0 because the expression has a null value. This is useful when you want to ensure that the number_of_cars question always contains a value, even if it is left blank.