Hi there! Today, I’d like to share the easiest way to write IF formulas in Salesforce. This became an issue because at some point I’ve been spending too much time on them. In most cases it was eaither missing parentheses or else statement. Epecially when formula field has been written by some one else and you have to update it.
Step 1: Understanding IF Formulas
So here is the basic IF formula:
Please also take a look at the help section from Salesforce located here.
So from the example above we test if certain condition evaluates to true, the formula will return ‘value_if_true‘, otherwise it will return ‘value_if_false‘.
Here is exact same formula but in graphic:
Let’s write a basic formula as an example. Say we have the following fields:
Invoice Due Date (Type: Date Formula) – this is where we’ll display the result;
Custom Checkbox (Type: Checkbox) – Custom_checkbox__c;
Delivery Date (Type: Date) – Delivery_Date__c;
We have the requirement to determine what will be our ‘Invoice Due Date‘. If ‘Delivery Date‘ is not empty, then ‘Invoice Due Date‘ formula should display the ‘Delivery Date‘. Otherwise, it will display nothing.
Step 2: How to do Multiple IF ELSE Statements In a Formula Field?
Let’s try to model the situation where we need to have multiple if-else conditions in our formula.
As an example: if a ‘Delivery Date‘ is not empty then show ‘Delivery Date‘. If ‘Delivery Date‘ AND ‘Custom Checkbox‘ is checked, then add 10 more days to an ‘Invoice Due Date‘
So this time, when first criteria evaluates to true I have to add another IF statement. For our second IF rule we no longer need to check if ‘Delivery Date’ is not empty because its already verified in first IF satement.
Obviouslty you may have as many IF satements as you want. You are only limited by the formula size.
Step 3: Best Practice On Writing IF Formulas
When the formula gets more and more IF statements it gets hard to track all the parentheses and else statements. For that reason I’m suggesting to use universal variable – NULL.
A NULL variable can temporarily fill the void in formula until you figure out the necessary condition.
Let’s take as a exmple our previous formula. We knew from the beginning that our first TRUE statement will be broken down into another IF. So let’s see how it’d look like with NULLs:
Let’s add one more IF to our first FALSE value:
The formula will always validate because you already have all 3 components set for each IF. And in case you’ve missed coma or something, you can always do a step back.
With this approach, you don’t have to worry about syntax that much. You just need to enter TRUE/FALSE values in place of NULL.
And again, the trick here is to pre-build a formula with NULL values for all TRUE/FALSE conditions. Once done, replace all necessary NULLs with actual values.
I’m hoping this method can save you some time while working with IF type formulas.