WEBcnx 2023.2 is out now bringing with it a host of new features and performance enhancements. Read more.

Open navigation

SWITCH Function

Applies from Version:WEBcnx 2024.1Applies to Version:


Description

The SWITCH function is somewhat similar to IF, but instead of simply checking if its first parameter is true or false, it evaluates the parameter and allows for multiple different outcomes.


Syntax 

SWITCH(inputValue, value1, result1, value2, result2)


SWITCH(inputValue, value1, result1, value2, result2, value3, result3)


SWITCH(inputValue, value1, result1, value2, result2, value3, result3, defaultResult)


The SWITCH function has the following arguments:


  • inputValue Required - The value we evaluate
  • value1 and result1 Required - A value and result pairing. If inputValue is equal to value1 , this function returns result1
  • value2 and result2 Optional - As above. If inputValue doesn't equal value1 , we check if it equals value2. We can define pairs of values and results in this manner indefinitely
  • defaultResult Optional - if inputValue doesn't match any of the values from our value/result pairings, we can return this defaultResult as a fallback.

Returns

Just like IF, the return type varies based on the parameters we pass into the function.


Remarks

  1. The SWITCH function requires at least three parameters but will usually have many more.
  2. The first parameter inputValue is required, and is analogous to the condition parameter in the IF function - it's there to be evaluated and determines what we return. However unlike IF, it can be of any type, not just a Boolean.
  3. After that, parameters come in pairs. The first of each pair is the value we're looking out for, the second is the value we return.
    If we follow that pattern, it's reasonable to always expect there to be an odd number of parameters (inputValue plus a series of pairs). We can however add one final parameter on, defaultResult which serves as a default "fallback" value if none of our pairs match inputValue
  4. Input parameters of System types are converted to their corresponding Api types. For instance, if System.String is given, it will be returned as Api.String.


Examples

Imagine we have an integer field with the Identifier MyInt

ExpressionDescriptionResult
SWITCH(MyInt, 1, "1st", 2, "2nd", 3, "3rd")
Creates an ordinal string for the numbers 1, 2 and 3
1st, 2nd or 3rd, if MyInt is 1, 2 or 3, respectively. Returns null for any other MyInt value
SWITCH(MyInt, 1, "1st", 2, "2nd", 3, "3rd", MyInt.ToString() + "th")
Creates an ordinal string for any number
1st, 2nd or 3rd, if MyInt is 1, 2 or 3, respectively. Returns a string containing the MyInt value and the suffix "th" for any other value (4th, 5th, 6th, etc)


Further Reading

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.

You may like to read -