How to use IF and IF/ELSE conditions to control the flow of a recipe
Why do you need conditions?
One of the first challenges a new automation builder encounters is how to build decision points into an automation. Often, a builder’s first recipe will be a simple data sync. For example, syncing records of customer support from their CRM to their technical support platform, or leads from a marketing platform to their CRM.
Fairly quickly, you’ll encounter a need to treat one record differently from another. For example:
- If a support case is missing some required information, you need to go back to the case owner to get the missing details.
- You may need to determine if a record already exists in an app to decide whether to use a “Create new record” or “Update existing record” action
- If a quote includes a certain level of discount, it needs to be approved by an executive sponsor, while undiscounted quotes can be processed without approval.
To handle these sorts of decisions, you can use IF and IF/ELSE conditions.
What are IF and IF/ELSE conditions?
IF and IF/ELSE conditions are a special type of action that applies conditional logic to control the flow of a recipe. This type of action consists of:
- A conditional statement
A conditional statement that can be either true or false. You can build this statement from the data output of any other step in the recipe. For example, this condition checks whether a Salesforce Case includes a description.
- Actions if condition is true
A “block” of actions that will be executed whenever the condition is true.
- Actions if condition is false
A block of actions that will be executed whenever the condition is false — this is only applicable for IF/ELSE conditions
How to use IF and IF/ELSE conditions in a recipe
To show you how to use these conditions in a recipe, let’s work through an example that includes both types of conditions. In a recent post, we used the example of escalating customer support cases from Salesforce to Jira to show how to use Lookup Tables to transform data.
Here, we’ll revisit the same use case, but this time we’ll focus on the flow of actions in the recipe, rather than how data is mapped.
We’ll use conditional logic to accomplish two tasks in this recipe:
- Identify cases that are incomplete, and prompt the case owner to provide the missing information.
- Determine whether we need to create a new issue in Jira, or update an existing issue.
To follow along exactly with this example, you’ll need active connections to:
However, the general principles applied below will work, no matter which apps you use in your organization.
Create the recipe
Create a new recipe with Salesforce as the triggering app, and select New/updated record in Salesforce as the trigger, and Case as the object.
Use an IF condition to send incomplete cases back to the owner
For our technical support team to be able to work on an issue, the details need to be complete. If the case is missing a description, we want to prompt the case owner to go back and provide the missing details.
Select and configure the IF condition
Under the new actions menu, select the IF condition
We want our condition to identify cases that have no description. So find the Description field in the recipe data widget, and select the is not present condition.
Note: to read up more on the different types of conditions you can set, see the full docs on conditions.
Create actions for the IF condition when its true
Now we need to create actions in our IF block that will alert the issue owner to provide the missing description (i.e IF condition is true). First, get the details of the issue owner from Salesforce.
The details returned by Salesforce include the issue owner’s email address. We can use this to look up their Slack ID.
Finally, we can send the issue owner a Slack message, prodding them to go and provide the missing details. We can grab their Slack user ID from the previous step, and the case number from the trigger.
Test the results
Set the recipe to Test mode, then create a case in Salesforce, but leave the description field blank.
You should see that your test succeeded, and your IF block executed.
You should also be able to see the notification in Slack.
Return the recipe to Test mode. Head back to Salesforce and update the case with a description.
The test should show the updated description field in the Output tab of the trigger, and you should see that the IF condition was not met, so the actions did not execute.
Use an IF/ELSE condition to choose between creating or updating an issue in Jira
Whenever our recipe picks up a new case in Salesforce, we want to create an equivalent issue in Jira, so let’s create that step.
We’ll map the subject and description fields from the Salesforce case to their equivalent fields in Jira. Note that whenever two apps touch the same underlying record, it’s important to have a unique identifier that works in both places. We can do that by setting the Salesforce case number as a label on the Jira issue.
However, this action by itself will not be enough to keep our customer support issue up-to-date across Salesforce and Jira. If we start the recipe like this, then every time a customer service rep updates a Salesforce case, we will create a brand new issue in Jira. That’s no good.
Setting up an IF/ELSE condition
What we need is a recipe that will create a Jira issue only if it doesn’t already exist, and update an issue if it does exist. We can accomplish this with an IF/ELSE condition.
First we need to search for an existing issue in Jira. This is where having that unique identifier comes in handy:
The Jira search will return a list of results. If no matching issue is found, that list will have a size of 0. We can use this to create our IF/ELSE condition.
Drag the original Create issue in Jira step into the IF block.
In the ELSE block, create a new Update issue in Jira action. Setup the Update action exactly like the Create action, except that when updating an existing record, we need the unique identifier of the record we want to update. If a record exists, this ID will be returned by the Search issues action.
Test the results
Testing the IF block
To test the IF/ELSE block, set the recipe back to Test mode and create a new issue in Salesforce. Make sure that the required Description field is present.
You should see a successful test with a newly created issue. Note the issue key and ID.
You can also check the created issue in the Jira UI.
Testing the ELSE block
Now we need to test the ELSE half of our description. To do this, we can update the existing issue. Our recipe should detect that a matching issue record already exists, and update it, instead of creating a new issue.
Return the recipe to Test mode. Then make an update to the case in Salesforce, such as adding more information to the Description field.
Your test should show that your IF condition was not met, so the ELSE block executed, updating the existing issue in Jira.
You should also be able to see the updated issue in Jira, with the original issue key.
If a duplicate issue has been created in Jira, something is wrong. Check that:
- You’re correctly setting the Salesforce case number as a label in Jira.
- Your Search issues action is correctly configured to search by issue labels.
Learn more about conditions
In this post, we’ve covered how to use conditions in IF and IF/ELSE actions to control the flow of the recipe. To learn more:
- Read up on all the different types of conditions you can create
- Discover other places that you can use conditional logic in your recipes, including trigger conditions and formulas.