0

Set multiple conditional statements

if ... then ... else if | switch ... case

Create multiple conditional statements by concatenating if-then-else blocks. Write another if after else for another condition and so on—continue for as long as needed.

Example

As in the previous example, use the Total number field again and insert the following script into a formula field:

if Total = null then
"Please enter an amount!"
else if Total >= 30 then
"Card" else "Cash" end
end

Result: In the function field you will receive the response "Cash", "Card", or "Please enter an amount!" according to the input.

Replace multiple nested conditional statements with switch ... case. Use this statement to query an expression (switch ...) for possible results (case ...:) and set a default behavior (default:) if the expression does not correspond to any of the results.

Depending on the result, assign the next step accordingly. This helps avoid deeply nested if statements.

ℹ️ Tip: switch ... case works best when the queried value is a choice field.

Example

There's a choice field Payment method with the following options:

  1. Cash
  2. Bank transfer
  3. Direct debit

Insert the following script into a formula field to display info about the selected payment type:

switch 'Payment method' do
case 1:
"Payment method: " + text('Payment method') + "."
case 2:
"Payment method: " + text('Payment method') + ". Only from €30."
case 3:
"Payment method: " + text('Payment method') + ". Do not forget your signature."
default:
"Please select a payment method."
end

Result: Based on your entry in the Payment method choice field, exactly one of the following info is visible in your formula field:

  • Payment method: Cash
  • Payment method: Bank transfer. Only from €30.
  • Payment method: Direct debit. Do not forget your signature.
  • Please select a payment method.

ℹ️ Do you want to dive deeper into the if ... then ... else topic? Take a look at the corresponding part of this video tutorial. For the switch topic, take a look at the corresponding part of the same video tutorial.

Reply

null