# Add conditions to formulas
It is also important to create recipes that are resilient against unexpected scenarios. For example, your trigger data might contain missing values or contain a data of another datatype.
You can use conditional logic to prepare your recipes for these situations.
# Conditionals
You can conditionally execute formulas using Ruby's ternary syntax (popular shortcut for if-else statements). Ternary syntax are of the form:
condition ? expression1 : expression2
# Behavior
# condition
A boolean expression that evaluates to true
or false
.
# expression1
Returns this expression if condition
is true
.
# expression2
Returns this expression if condition
is false
.
# Example: Using first name or full name
In the following example, we conditionally pass in either the Full name or First name into the Message input field.
Checks if Full name is present. Outputs Full name if present, or First name if not present.
Here is a detailed explanation of what the ternary formula does:
Full name.present?
will check if the Full name pill has a value . If it has a value, it evaluates totrue
. If it has no value, it evaluates tofalse
.- The second
?
in the formula separates the condition to evaluate from the expressions to return. Note, the first?
is part of the.present?
formula whilst the second?
is separated with a space character and is part of the ternary syntax. - If there is a value in the Full name pill when the job is ran, the value for Full name will mapped to the Message input.
- If there is no value in the Full name pill when the job is ran, the value for First name will be mapped to the Message input. Of course, if there's also no value in this First name pill, the job will fail at this step if Message is a required input field.
For more information on Ruby's ternary syntax, check out this article (opens new window).
# Example: Skip field if empty
When updating records, you want to preserve existing data while changing only the updated fields. In this situation, can you use the skip
formula to instruct the Workato action to leave this field untouched.
This example attempts use an updated Salesforce record to update a lead in Marketo. It checks if the Salesforce Company is present. If yes, it will output the Salesforce Company into Marketo. Otherwise, the Marketo record is left untouched.
Checks if Company is present. Outputs Company if present, otherwise, leaves this field untouched
How to avoid passing any values
The skip formula will avoid passing any data to the input field.
# Safe Navigation Operator
Checks if the input is valid. If true, it performs a specified operation on the input data. Otherwise, it will return a null
value.
# Syntax
Input&.operation
- Input - An input datapill. It can by any datatype.
- operation - If the input is not
null
, this formula will be applied to the input data. This formula must be compatible with the input datatype.
# How it works
The Safe Navigation Operator (&.
) checks if the input data is valid. It returns a null
value if the input data is null
or undefined. If the input is valid, it will perform the operation specified by the operation argument. It enables you to write simpler expressions to handle null
values.
- Before
The Created Date datapill needs to be converted to a date datatype using the to_date
formula. Since the formula will encounter an error if the datapill contains null
value, you would have to use a ternary expression to work around this logic.
Using ternary conditions
- After
Use the &.
operator instead. The safe navigation operator allows you to manage cases where Created Date contains a null
value, while writing a simpler formula expression.
Using Save navigation operator
Last updated: 4/5/2023, 11:28:53 AM