# 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 not nil
or undefined. If true, it performs a specified operation on the input data. Otherwise, it returns a nil
value.
# Syntax
Input&.operation
- Input - An input datapill. It can be any datatype.
- operation - If the input is not
nil
, this formula is 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 not nil
or undefined. If the input data exists, it performs the operation specified by the operation argument. Otherwise, it returns nil
. This operator enables you to write simpler expressions to handle nil
values.
# Example: Converting dates safely
You can use the safe navigation operator when you're working with a datapill that might contain nil
values.
For example, applying the to_date
formula directly to the Created Date Step 1 datapill would cause a NoMethodError
if the datapill value is nil
. The safe navigation operator enables you to handle these cases without complex ternary expressions.
# Instead of: created_date.present? ? created_date.to_date(format:"MM/DD/YYYY") : nil
created_date&.to_date(format:"MM/DD/YYYY")
Using the safe navigation operator
# Example: Safe hash retrieval
You can also use the safe navigation operator when you have a nested hash structure, such as a customer record with optional address information.
With safe navigation:
# This returns nil instead of an error if any key is missing
customer["address"]&.[]("billing")&.[]("zip_code")
Without safe navigation:
# This could fail if "address" or "billing" doesn't exist
customer["address"]["billing"]["zip_code"]
The safe navigation operator prevents errors by returning nil
if any key is missing. This approach allows you to retrieve nested hash values without worrying about NoMethodError
exceptions.
Last updated: 7/18/2025, 12:15:10 AM