Skip to main content

Switch Task

"type" : "SWITCH"

Introduction

A switch task is similar to case...switch statement in a programming language. The switch expression is a configuration on the SWITCH task type. Currently, two evaluators are supported:

namedescription
value-paramUse a parameter directly as the value
javascriptEvaluate Javascript expressions and compute the value

Use Cases

Useful in any situation where we have to execute one of many task options.

Configuration

Following are the task configuration parameters :

nametypedescription
evaluatorTypeStringType of the evaluator used. Supported types: value-param, javascript, graaljs.
expressionStringThe expression that depends on the evaluator type. For the value-param evaluator, the expression is the input parameter; for the javascript evaluator, it is the javascript expression.
decisionCasesMap[String, List[task]]Map where the key is possible values that can result from the expression being evaluated by evaluatorType with the value being the list of tasks to be executed.
defaultCaseList[task]List of tasks to be executed when no matching value is found in decision case (default condition)

Output

Following is/are output generated by the Switch Task.

nametypedescription
evaluationResultList[String]A List of strings representing the list of cases that matched.

Examples

In this example workflow, we have to ship a package with the shipping service providers on the basis of input provided while running the workflow.

Let's create a Workflow with the following switch task definition that uses value-param evaluatorType:

{
"name": "switch_task",
"taskReferenceName": "switch_task",
"inputParameters": {
"switchCaseValue": "${workflow.input.service}"
},
"type": "SWITCH",
"evaluatorType": "value-param",
"expression": "switchCaseValue",
"defaultCase": [
{
...
}
],
"decisionCases": {
"fedex": [
{
...
}
],
"ups": [
{
...
}
]
}
}

In the definition above, the value of the parameter switch_case_value is used to determine the switch-case. The evaluator type is value-param and the expression is a direct reference to the name of an input parameter. If the value of switch_case_value is fedex, then the decision case ship_via_fedex is executed as shown below.

Conductor UI - Workflow Run

Similarly, if the input was ups, then ship_via_ups will be executed. If none of the cases match, then the default option is executed.

Here is an example using the javascript evaluator type:

{
"name": "switch_task",
"taskReferenceName": "switch_task",
"inputParameters": {
"inputValue": "${workflow.input.service}"
},
"type": "SWITCH",
"evaluatorType": "javascript",
"expression": "$.inputValue == 'fedex' ? 'fedex' : 'ups'",
"defaultCase": [
{
...
}
],
"decisionCases": {
"fedex": [
{
...
}
],
"ups": [
{
...
}
]
}
}

Codelab examples