Switch
The Switch task is used for conditional branching logic. It represents if...then...else or switch...case statements in programming, and can be used for any situation where different tasks have to be executed based on different conditions.
A Switch task evaluates an expression, either a simple input parameter key or a complex JavaScript expression, and matches the expression output with the name of each switch case. The appropriate tasks are executed based on the matching branch, which contains a sequence of tasks. The default branch will be executed if no matching branches are found.
Task parameters
Configure these parameters for the Switch task.
Parameter | Description | Required/ Optional |
---|---|---|
evaluatorType | The type of evaluator used. Supported types:
| Required. |
expression | The expression that is evaluated by the Switch task. The expression format depends on the evaluator type:
| Required. |
decisionCases | A map of the possible switch cases. The keys are the possible outputs of the evaluated expression, and the values are the list of tasks to be executed in each case. | Required. |
defaultCase | The default branch. Contains the list of tasks to be executed when no matching value is found in the decision cases. | Optional. |
Task configuration
This is the task configuration for a Switch task.
- value-param
- graaljs
{
"name": "switch",
"taskReferenceName": "switch_ref",
"inputParameters": {
"switchCaseValue": "${workflow.input}"
},
"type": "SWITCH",
"decisionCases": {
"caseName1": [
{//taskDefinition}
],
"caseName2": [
{//taskDefinition},
{//taskDefinition}
]
},
"defaultCase": [
{//taskDefinition}
],
"evaluatorType": "value-param",
"expression": "switchCaseValue"
}
{
"name": "switch",
"taskReferenceName": "switch_ref",
"inputParameters": {
"switchCaseValue": "${workflow.input.num}"
},
"type": "SWITCH",
"decisionCases": {
"apples": [
{//taskDefinition}
],
"tomatoes": [
{//taskDefinition}
],
"oranges": [
{//taskDefinition}
]
},
"defaultCase": [],
"evaluatorType": "graaljs",
"expression": "(function () {\n switch ($.switchCaseValue) {\n case \"1\":\n return \"apple\";\n case \"2\":\n return \"tomatoes\";\n case \"3\":\n return \"oranges\"\n }\n }())"
}
Task output
The Switch task will return the following parameters.
Parameter | Description |
---|---|
selectedCase | The evaluation result of the Switch task. |
Accessing the Switch task output
You can access the output of the Switch task in subsequent tasks by referring to the output value selectedCase
, using ${switchTaskName.output.selectedCase}
(replacing switchTaskName
with the actual task reference name).
Adding a Switch task in UI
To add a Switch task:
- In your workflow, select the (+) icon and add a Switch task.
- In Script params, add the parameter that will be evaluated in the expression.
- In Evaluate, select the evaluator type and enter the expression.
- Value-Param—Ensure that the expression value matches the parameter key you have defined in Script params.
- ECMASCRIPT—Enter a JavaScript script.
- In Switch cases, label the cases with the relevant parameter values.
- In your workflow, select the (+) icon to add tasks to each switch case.
- (Optional) Add tasks to the default case.
Examples
Here are some examples for using the Switch task.
Nested switch case
Similar to any programming language, you can use other operators inside a switch case, such as nested switches, loops, forks, and so on.
{
"name": "switch",
"taskReferenceName": "switch_ref",
"inputParameters": {
"switchCaseValue": "${workflow.input.shipping}"
},
"type": "SWITCH",
"decisionCases": {
"fedex": [
{//taskDefinition}
],
"ups": [
{//taskDefinition
"name": "nestedSwitch",
"taskReferenceName": "nestedSwitch_ref",
"inputParameters": {
"deliveryType": "${workflow.input.delivery}"
},
"type": "SWITCH",
"decisionCases": {
"same_day": [ {//taskDefinition}],
"regular": [ {//taskDefinition}]
},
"defaultCase": [],
"evaluatorType": "graaljs",
"expression": "$.deliveryType == 'same-day' ? 'same_day' : 'regular'",
}
]
}
"defaultCase": [],
"evaluatorType": "value-param",
"expression": "switchCaseValue"
}
JavaScript expressions
When using `javascript` or `graaljs` as the evaluator type, the `expression` can be a JavaScript expression that returns a string. Within the `expression`, the Switch task input parameter is available as the variable inside the $ scope.
Input parameters for a javascript or graaljs evaluator type:
"inputParameters" : {
"shippingType": "${workflow.input.shipping}"
}
Expression for a javascript or graaljs evaluator type:
((
function () {
if ($.shippingType == 'EXPRESS') {
return "FEDEX";
}
return "USPS";
}
))();
JavaScript expression that evaluates a datetime string
The script below returns `OLDER` or `NEWER` depending on whether the input date is older than one month.
((
function () {
const date = new Date($.timestamp);
const currentDate = new Date();
// Subtract one month from the current date
currentDate.setMonth(currentDate.getMonth() - 1);
if (date < currentDate) {
return "OLDER";
}
return "NEWER"
}
))();
Check out the sample workflow execution that runs this switch case in our Playground.