If Else
Rather than using traditional if...else if...else statements in JavaScript, you can visually create conditional logic with this activity. This approach simplifies complex JavaScript logic by breaking it down into more manageable components.
If activity evaluates the incoming data based on a series of defined conditions and follows the appropriate branch:
- The If branch is executed if its condition evaluates to a truthy value.
- If the If branch condition is falsy, the workflow evaluates ElseIf branches sequentially. The first ElseIf branch with a truthy condition is executed.
- If none of the If or ElseIf conditions are met, the Else branch is executed.
Every branch has conditions that determine whether it should be executed.
Condition
A condition is composed of three essential components, each of which helps in defining the logic for evaluation:
-
Field: This represents the data sourced from the workflow. It can refer to variables or results from other activities or payload. Examples include:
{{payload.name}}
{{api1.result.price}}
{{slack1.result.users}}
-
Condition (Operator): This is the list of operators used to compare the field against the provided data. You will find the list of all the supported operators below.
-
Data: This is the value against which the field is compared. The data can vary depending on the operator chosen:
- In some cases, data is required for comparison (e.g., for operators like equals, greater than).
- In other cases, data isn't required (e.g., is truthy, exists).
Operators
Operator | Meaning | Example |
---|---|---|
less than | Less than comparison | {{payload.age}} less than 18 |
greater than | Greater than comparison | {{api1.result.price}} greater than 100 |
less than or equal | Less than or equal to comparison | {{slack1.result.users}} less than or equal 50 |
greater than or equal | Greater than or equal to comparison | {{fn1.result.score}} greater than or equal 75 |
equal | Equality comparison (works with numbers and strings) | {{payload.age}} equal 18 or {{api1.result.status}} equal "success" |
strictly equal | Strict equality comparison (checks both value and type) | {{payload.age}} strictly equal 18 or {{slack1.result.channel}} strictly equal "general" |
does not equal | Inequality comparison (works with numbers and strings) | {{payload.age}} does not equal 18 or {{fn1.result.status}} does not equal "failed" |
strictly does not equal | Strict inequality comparison (checks both value and type) | {{payload.age}} strictly does not equal 18 or {{freshsales1.result.lead}} strictly does not equal "new" |
contains | Checks if string contains another string | {{payload.description}} contains "error" |
does not contain | Checks if string does not contain another string | {{api1.result.message}} does not contain "success" |
exactly matches | Exact match comparison (string comparison) | {{slack1.result.status}} exactly matches "completed" |
does not exactly match | Checks if strings do not exactly match | {{fn1.result.status}} does not exactly match "completed" |
is in | Checks if string is present in another string | {{payload.role}} is in "admin, user, guest" |
is not in | Checks if string is not present in another string | {{freshsales1.result.role}} is not in "admin, user, guest" |
starts with | Checks if string starts with another string | {{payload.name}} starts with "John" |
does not start with | Checks if string does not start with another string | {{api1.result.name}} does not start with "John" |
ends with | Checks if string ends with another string | {{slack1.result.name}} ends with "Doe" |
does not end with | Checks if string does not end with another string | {{fn1.result.name}} does not end with "Doe" |
matches regex | Matches string with a regular expression (JavaScript). Build Regex from here | {{payload.email}} matches regex "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$" |
does not match regex | Checks if string does not match a regular expression (JavaScript). Build Regex from here | {{api1.result.email}} does not match regex "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$" |
after | Checks if a date is after another date | {{payload.date}} after "2023-01-01T00:00:00Z" |
before | Checks if a date is before another date | {{api1.result.date}} before "2023-01-01T00:00:00Z" |
equals | Checks if two dates are equal | {{slack1.result.date}} equals "2023-01-01T00:00:00Z" |
is true | Checks if a value is exactly true (boolean) | {{payload.isActive}} is true |
is false | Checks if a value is exactly false (boolean) | {{api1.result.isActive}} is false |
exists | Checks if the field exists (is not null or undefined) | {{payload.name}} exists |
does not exist | Checks if the field does not exist (is null or undefined) | {{api1.result.name}} does not exist |
is truthy | Checks if the value is truthy (non-falsy value) | {{payload.value}} is truthy |
is falsy | Checks if the value is falsy (null, undefined, false, 0, NaN, "") | {{api1.result.value}} is falsy |
is array empty | Checks if the array is empty | {{payload.items}} is array empty |
is array not empty | Checks if the array is not empty | {{api1.result.items}} is array not empty |
length equals | Checks if the length of an array equals a specified Number | {{slack1.result.items}} length equals 5 |
length greater than | Checks if the length of an array is greater than a specified Number | {{fn1.result.items}} length greater than 5 |
length less than | Checks if the length of an array is less than a specified Number | {{freshsales1.result.items}} length less than 5 |
is object empty | Checks if an object is empty (no keys) | {{payload.details}} is object empty |
is object not empty | Checks if an object is not empty (has keys) | {{api1.result.details}} is object not empty |
object contains key | Checks if an object contains a specified key | {{slack1.result.details}} object contains key "name" |
object does not contain key | Checks if an object does not contain a specified key | {{fn1.result.details}} object does not contain key "name" |
is type of | Checks if the value is of a specific type (e.g., string, Number, etc.) | {{payload.age}} is type of "number" |
not type of | Checks if the value is not of a specific type (e.g., string, Number, etc.) | {{api1.result.age}} not type of "string" |
Additional Features
- Multiple ElseIf branches supported: You can define as many ElseIf conditions as needed, creating more complex branching logic.
- Multiple conditions in any branch: Within the If or ElseIf branches, you can specify multiple conditions, which are connected using AND or OR logic to refine the evaluation.
Advanced Options
For more information on advanced settings, see the Advanced Options documentation.