Sub topic 4
Combining Conditions with Logical Operators and Functions
This chapter assumes you can open a validation rule in the Object Manager and that you know the rule's formula must evaluate to true when the data is invalid. It also assumes you can read spreadsheet-style formulas. Here you will combine several tests into one formula and avoid the mistakes that make combined formulas misbehave.
Why one condition is rarely enough
Real business rules have exceptions. "Amount must be positive" becomes "Amount must be positive once the deal reaches Negotiation/Review." That sentence has two tests: the stage and the amount. The logical functions join such tests into one true-or-false result.
The rule fires only when the whole formula returns true. Each logical function reduces several inputs to one answer.
The core logical functions
| Function | Returns true when | Notes |
|---|---|---|
| `AND(a, b, ...)` | every argument is true | Needs at least two arguments |
| `OR(a, b, ...)` | at least one argument is true | Needs at least two arguments |
| `NOT(a)` | the argument is false | Takes exactly one argument |
| `IF(test, x, y)` | not applicable | Returns `x` if the test is true, otherwise `y` |
| `CASE(expr, v1, r1, ..., else)` | not applicable | Compares one value against several literals |
Salesforce also accepts the operators && (and), || (or) and ! (not). The functions are easier to read once a formula grows. They are also easier to indent over several lines.
Reading AND and OR as a rule's shape
Think of AND as "all of these problems are present at once" and OR as "any one of these problems is enough."
AND(
ISPICKVAL(StageName, "Closed Won"),
OR(ISBLANK(Amount), CloseDate > TODAY())
)This rule fires for a Closed Won opportunity that has no amount or a close date in the future. The stage test must always be true. Either of the two problems then triggers the error.
The diagram shows how the formula reduces from the inside out.
Using NOT and De Morgan's laws
Business rules are usually stated as what must be true. Validation rules need the opposite, which is the failure case. You can invert a condition in two ways.
- Wrap the whole expression in
NOT(...). - Rewrite it by pushing the negation inward.
The second approach follows De Morgan's laws, and the two forms are equivalent:
| Wrapped form | Pushed-in form |
|---|---|
| `NOT(AND(a, b))` | `OR(NOT(a), NOT(b))` |
| `NOT(OR(a, b))` | `AND(NOT(a), NOT(b))` |
Suppose the requirement is "a Contact must have a phone or an email." State the valid case first: OR(NOT(ISBLANK(Phone)), NOT(ISBLANK(Email))). The invalid case is the negation of that. Pushing the NOT inward gives a shorter rule.
AND(
ISBLANK(Phone),
ISBLANK(Email)
)Writing the valid case first and then inverting it is a reliable way to avoid logic mistakes. Check the result against a row where both fields are filled, a row where only one is, and a row where neither is.
Precedence and parentheses
&& binds more tightly than ||, as in most languages. A formula such as a || b && c therefore means a || (b && c). People often read it the other way. Avoid the problem in two ways:
- Prefer the
ANDandORfunctions, where the argument lists make grouping explicit. - If you use the symbolic operators, add parentheses even where they are not required.
Branching with IF
IF returns one of two values. In a validation rule the final result must be a true or false value, so each branch must also evaluate to true or false. This is useful when the threshold depends on another field.
IF(
Amount > 50000,
Discount_Percent__c > 20,
Discount_Percent__c > 10
)This rule assumes a custom number field named Discount_Percent__c. It allows a discount of up to 20 percent on deals over 50,000 and up to 10 percent on smaller ones. The rule fires when the discount exceeds the limit for the deal size.
You can write the same logic with AND and OR. Pick the form that reads more naturally for your rule.
OR(
AND(Amount > 50000, Discount_Percent__c > 20),
AND(Amount <= 50000, Discount_Percent__c > 10)
)IF can nest, but deep nesting is hard to review. Past two levels, consider CASE.
Replacing repeated tests with CASE
CASE compares one expression against a list of literal values and returns the matching result. Use it instead of a chain of OR tests on one field. This Lead rule requires a description when the status is one of two values:
AND(
CASE(
Status,
"Working - Contacted", 1,
"Closed - Not Converted", 1,
0
) = 1,
ISBLANK(Description)
)The final 0 is the default result. CASE works on picklist fields directly, so you do not need ISPICKVAL inside it. The picklist chapter covers this in more detail. Note that the values you compare against must be the picklist's API values, spelled exactly.
Other functions that appear in combined rules
Most real rules mix the logical functions with a few others.
| Function | Typical use |
|---|---|
| `ISBLANK(field)` | Test for an empty field |
| `LEN(text)` | Check a text length, for example `LEN(Description) < 20` |
| `CONTAINS(text, part)` | Look for a substring. This is case-sensitive |
| `BEGINS(text, prefix)` | Check how a value starts |
| `TODAY()` | Compare a date against the current date |
| `ABS(number)` | Compare a size of difference regardless of sign |
The = operator on text is case-insensitive, while CONTAINS is case-sensitive. A rule can therefore match "usa" with = but miss it with CONTAINS.
Common errors
Too few arguments
AND(a) and OR(a) fail to save. If you only have one test, drop the wrapper.
Parentheses that don't balance
Long formulas lose a closing parenthesis easily. Put each argument on its own line and keep the closing parenthesis aligned with its opening function, as in the examples above. The Check Syntax button reports the error but not always where you meant it.
Returning the wrong type
IF(a, "yes", "no") returns text, and a rule needs a Boolean. Use IF only when both result branches are conditions.
Assuming evaluation stops early
Programming languages often skip the rest of an AND once one part is false. Do not rely on that in Salesforce formulas.
Formula size
A validation formula is limited to 5,000 characters. Heavy duplication of tests, such as the second form of the discount rule, uses that budget quickly. If a rule approaches the limit, split it into two rules, each with its own message. You can also move the logic into a flow or Apex, which are covered in a later chapter.
Forgetting blanks
Amount <= 0 and ISBLANK(Amount) test different things, and a combined formula often needs both. That is why the first example in the spine pairs them with OR. The next chapter treats blank handling in full.
A working method for combined rules
- Write the requirement as a plain sentence.
- List each separate test it contains.
- Decide which tests must all be true (
AND) and which are alternatives (OR). - Invert the valid case so the formula is true for bad data, using the table above if it helps.
- Check the formula against rows for each branch, including a row that should pass.
Always include a passing row in your checks. A rule that blocks bad data but also blocks good data is the most common production defect.
Exercise
A Contact must have a phone or an email. Write the error condition formula that fires only when both are missing.
Show answer
AND(ISBLANK(Phone), ISBLANK(Email)). It is true only when both fields are empty. If either holds a value, AND returns false and the save goes through.
Exercise
Rewrite NOT(OR(ISBLANK(Phone), ISBLANK(Email))) using AND and no outer NOT. In plain words, when does the original fire?
Show answer
AND(NOT(ISBLANK(Phone)), NOT(ISBLANK(Email))). By De Morgan's laws the two are equivalent. Both fire when the Contact has both a phone and an email, which is probably not what the author intended, so the original is likely a bug.
Exercise
Why is AND(Quantity_c <> 0, Totalc / Quantity_c > 100) unsafe, and how do you fix it?
Show answer
Salesforce does not guarantee that the division is skipped when the first test is false, so a zero quantity can still cause a division-by-zero error. Use IF(Quantity_c = 0, FALSE, Totalc / Quantity_c > 100) so the division sits in the branch that runs only for a nonzero quantity.