Add branching logic to a job with the If/else task
Use the If/else condition
task to add boolean conditional logic to task graphs. These tasks consist of a boolean operator and a pair of operands, where the operands might reference the job or task state using configured or dynamic parameters or task values. See Parameterize jobs.
For example, suppose you have a task named process_records
that maintains a count of records that are not valid in a value named bad_records
, and you want to branch processing when you encounter bad records. To add this logic to your workflow, you can create an If/else condition
task with an expression like {{tasks.process_records.values.bad_records}} > 0
. You can then add dependent tasks based on the results of the condition.
After a job run containing an If/else condition
task, you can view the result and the expression evaluation details when you view the job run details in the UI. See View job run details.
Note
- Numeric and non-numeric values are handled differently depending on the boolean operator:
- The
==
and!=
operators perform string comparison of their operands. For example,12.0 == 12
evaluates to false. - The
>
,>=
, and<=
operators perform numeric comparisons of their operands. For example,12.0 >= 12
evaluates to true, and10.0 >= 12
evaluates to false. - Only numeric, string, and boolean values are allowed when referencing task values in an operand. Any other types will cause the condition expression to fail. Non-numeric value types are serialized to strings and are treated as strings in
If/else condition
expressions. For example, if a task value is set to a boolean value, it is serialized to"true"
or"false"
.
- The
Configure an If/else task
Add an If/else condition
task from the Tasks tab in the Jobs UI by doing the following:
- In the Type drop-down menu, select
If/else condition
. - Enter the operand to be evaluated in the first Condition text box. The operand can reference any of the following:
- A job parameter variable.
- A task parameter variable.
- A task value.
- Select a boolean operator from the drop-down menu.
- In the second Condition text box, enter the value for evaluating the condition.
- Click Save task.
Configure dependencies on an If/else condition
Configure dependencies on the If/else condition
task from the tasks graph in the Tasks tab by doing the following:
- Select the
If/else condition
task in the tasks graph and click + Add task. - Enter details for the task. The Depends on field defaults to
<task-name> (true)
where<task-name>
is the name of theIf/else condition
task.- Select
<task-name> (false)
to configure a task that runs on a false condition evaluation.
- Select
You can configure multiple tasks to run in serial or parallel based on the outcome of an If/else condition
. Consider configuring Run if dependencies
if you need conditionalized runs based on upstream task failures. See Configure task dependencies.