The Switch Task allows you to choose tasks based on input values. This task is useful for controlling the flow of your workflow by executing different sets of tasks depending on the evaluated expression.
This is similar to the switch statement in programming languages, where you can choose different cases based on the value of an expression.
To use the Switch Task, you need to define it in your workflow file and specify the expression to evaluate, the decision cases, and the default case.
You can't define a
tasksfield in the Switch Task. Instead, you should use thedecisionCasesanddefaultCasefields to specify the tasks to execute based on the evaluated expression.
Here is an example of a Switch Task in a workflow file:
{
"name": "sample-workflow",
"tasks": [
{
"name": "choose_case",
"handler": "switch",
"parameters": {
"expression": "${input.value}"
},
"decisionCases": {
"case1": [
{
"name": "log_case1",
"handler": "log",
"parameters": {
"message": "Case 1 executed"
}
}
],
"case2": [
{
"name": "log_case2",
"handler": "log",
"parameters": {
"message": "Case 2 executed"
}
}
]
},
"defaultCase": [
{
"name": "log_default",
"handler": "log",
"parameters": {
"message": "Default case executed"
}
}
]
}
]
}expression: The expression to evaluate. The result of this expression will be matched with the target case. Theexpressioncan be written in any language supported by theScript Engine, we support javascript and python by default.language: The language of the expression. Default isjavascript.
decisionCases: An object containing case-task mappings. The tasks in the matched case will be executed.defaultCase: An array of tasks to execute if no case matches the evaluated expression. The default case is not required, but it is recommended to handle cases where no case matches the evaluated expression.
The Switch Task will evaluate the expression and execute the tasks specified in the matched case or the default case if no case matches.
The result of this task will be the evaluated expression result.
The Switch Task allows you to control the flow of your workflow by choosing tasks based on input values.
Define the expression using the expression and language parameters, and specify the tasks to execute in the decisionCases and defaultCase properties.