Sub topic 4
Branching with IF and CASE
Formulas become useful once they can make decisions. This chapter covers the two main branching functions, IF and CASE. It shows how to combine conditions with AND and OR, and how to choose between the functions. It also covers the mistakes that most often make a formula fail to save.
This chapter assumes you can create a formula field in Setup and know the return types from earlier chapters: Text, Number, Date, Checkbox and so on. It also assumes you are comfortable with the comparison operators =, <>, > and <.
How IF works
The syntax is IF(logical_test, value_if_true, value_if_false). Salesforce evaluates the test. If it is true, the formula returns the second argument. Otherwise it returns the third.
Nesting an IF in the false position gives you more than two outcomes. The following formula is a Text formula field on Opportunity that buckets deal size.
IF(BLANKVALUE(Amount, 0) > 100000, "Large",
IF(BLANKVALUE(Amount, 0) > 10000, "Medium", "Small"))The tests run from top to bottom, and the first true test wins. An Amount of exactly 100000 is not greater than 100000, so it returns "Medium". If you want 100000 to count as "Large", use >=. Boundary values are the most common source of off-by-one errors in tiered formulas.
Combining conditions
Use AND, OR and NOT to build compound tests. The operators &&, || and ! do the same thing.
IF(AND(IsClosed, IsWon), "Won",
IF(IsClosed, "Lost", "Open"))IsClosed and IsWon are Checkbox fields, so they can be used directly as tests. You do not need to write IsClosed = TRUE.
Picklists need ISPICKVAL
You cannot compare a picklist field to text with =. In an IF, wrap the field in ISPICKVAL, or convert it with TEXT.
IF(ISPICKVAL(StageName, "Closed Won"), "Done", "In progress")Equivalent: IF(TEXT(StageName) = "Closed Won", "Done", "In progress").
How CASE works
CASE compares one expression against a list of possible values. Its syntax is CASE(expression, value1, result1, value2, result2, ..., else_result). The last argument is the default. It is required, and it is returned when nothing matches.
CASE(StageName,
"Prospecting", 10,
"Qualification", 25,
"Closed Won", 100,
0)This is a Number formula field. A stage such as "Negotiation" matches none of the listed values, so it returns 0. CASE accepts a picklist field directly, so no ISPICKVAL is needed.
Choosing between IF and CASE
| Situation | Better choice | Reason |
|---|---|---|
| Exact matches against one field | CASE | One expression is named once, and each branch is a short pair. |
| Ranges such as `> 10000` | IF | CASE only tests equality. |
| Tests on different fields | IF | CASE has a single expression. |
| Compound conditions with AND or OR | IF | CASE has no way to express them. |
| Many branches | CASE | It stays flat, while a nested IF grows deeper and longer. |
The same four-way stage mapping written as nested IFs would need ISPICKVAL in every level. The CASE version is easier to read and leaves less room for a typo.
Rules that cause most errors
- All results must share one data type. Every result in an IF or CASE must match the formula's return type.
IF(Amount > 0, "Yes", 1)fails to save, because one branch is Text and the other is a Number. - Every IF needs a false branch. There is no one-armed IF. For a "do nothing" case, return
""in a Text formula,0orNULLin a Number formula, orNULLin a Date formula.NULLis not valid in every context, so test it when you save the field. - CASE needs a default. If you leave off the final else value, the formula editor reports an error about the number of arguments.
- CASE cannot test ranges.
CASE(Amount, > 100, ...)is not valid. Use IF, or convert the range into a discrete value first, for example withFLOORorMONTH. - Checkbox formulas return true or false. You do not need
IF(condition, TRUE, FALSE). Write the condition by itself.
Order matters and guards go outside
Because the first true test wins, order nested IFs from most specific to least specific. A broad test placed first hides every branch after it.
/* Wrong order: every deal over 10000 returns "Medium", including those over 100000 */
IF(BLANKVALUE(Amount, 0) > 10000, "Medium",
IF(BLANKVALUE(Amount, 0) > 100000, "Large", "Small"))Do not rely on AND or OR to short-circuit. In AND(NOT(ISBLANK(Amount)), Amount / Quantity > 5), the division can still be evaluated when the first test is false. Put the guard in an outer IF so the risky expression sits only in the branch where it is safe. The next chapter applies this idea to blanks and division by zero.
Keeping formulas readable
- Add comments with
/* ... */. They are allowed in formulas and do not affect the result, but they do count toward the size limit. - Indent nested IFs and put one branch per line.
- Prefer CASE once a nested IF goes more than three levels deep.
- Remember that long formulas hit size limits. Those limits get their own chapter.
Exercise
A Number formula field on Opportunity must return a discount percentage based on Type, a picklist. "New Customer" returns 5, "Existing Customer - Upgrade" returns 10, and every other value returns 0. Separately, a second Text formula must return "Hot" when Amount is above 50000 and the stage is not Closed Lost, and "Normal" otherwise. Which function suits each formula, and how would you write them?
Show answer
The first formula suits CASE, because it is exact matches on one field: CASE(Type, "New Customer", 5, "Existing Customer - Upgrade", 10, 0). The second needs a range and a compound condition, so it uses IF: IF(AND(BLANKVALUE(Amount, 0) > 50000, NOT(ISPICKVAL(StageName, "Closed Lost"))), "Hot", "Normal").
Exercise
This Text formula will not save. What is wrong? IF(Amount > 1000, "Big", 0)
Show answer
The branches return different data types. "Big" is Text and 0 is a Number, but every result in an IF must match the return type. Change 0 to "Small" (or any text value).