Skip to main content

Switch

The switch task is used for creating branching logic. It is a representation of multiple if...then...else or switch...case statements in programming.

Definitions

{
"name": "switch_task",
"taskReferenceName": "switch_task_ref",
"inputParameters": {
"switchCaseValue": "${workflow.input.service}"
},
"type": "SWITCH",
"evaluatorType": "value-param",
"expression": "switchCaseValue",
"defaultCase": [//tasks],
"decisionCases": {
"fedex": [//tasks],
"ups": [//tasks]
}
}
  • A switch task takes an expression as input along with multiple branches containing a sequence of tasks to be executed and a default branch to be executed if no matching branches are found.
  • The output of the expression is matched with the name of the branch.
  • The expression can be a javascript expression or a value parameter that represents the input to the task directly.

Input Parameters

AttributeDescription
evaluatorTypeIndicates the type of evaluator used. Supported types are value-param, javascript, and graaljs.
expressionThe expression depends on the evaluator type. For the value-param evaluator, the expression is the input parameter; for the javascript and graaljs evaluator, it is the javascript expression.
decisionCasesMap where the key is possible values that can result from the expression, with the value being the list of tasks to be executed.
defaultCaseList of tasks to be executed when no matching value is found in the decision case (default condition).

Types of Evaluators

AttributeDescription
value-paramUse a parameter directly as the value.
javascriptEvaluate Javascript expressions and compute the value - Legacy. Deprecated - use graaljs instead.
graaljsEvaluate Javascript expressions and compute the value. Allows you to use ES6-compatible Javascript.

Examples



  1. Add task type Switch.
  2. Click on the (+) icon to add switch cases.
  3. Add the value parameter to evaluate for switch.
  4. Label the cases with values to match.
  5. Add one more task to the cases.
  6. Add tasks to default case if applicable.

Adding event task

Access Switch Case Output

We can access the output of the switch case in subsequent tasks by referring to the output value selectedCase. For example, if the switch case reference was switch_example_1 we can access the output value by:

${switch_example_1.output.selectedCase}

Using Javascript Expressions

When using javascript or graaljs as the evaluator type, the expression can be a javascript expression that returns a string.

The input to the tasks is available as the variables inside the $ scope within the script.

    "inputParameters" : {
"shippingType": "${workflow.input.shippingType}"
}

(function () {
if ($.shippingType == 'EXPRESS') {
return "FEDEX";
}
return "USPS";
})();

Nested Switch case

Switch task can be nested just like nested if...then...else.

{
"decisionCases": {
"fedex": [//tasks],
"ups": [
{
"taskType": "SWITCH",
"expression": "$.deliveryType == 'same-day' ? 'same_day' : 'regular'",
"decisionCases": {
"same_day": [],
"regular": [],
}
}
]
}
}

tip

Similar to any programming language, you can have other operators inside a switch case, such as nested switches, loops, forks, etc.

FAQs

How can I create a JavaScript switch case statement that evaluates whether a given datetime string is older than one month?

You can use the following script:

((
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"
}
))();

It returns “OLDER” or “NEWER” depending on the input date. Check out the sample workflow execution that runs this switch case in our playground.