Sub topic 4
Combining conditions with logical operators and functions
This chapter assumes you can open Object Manager, create a validation rule, and read spreadsheet-style formulas. A validation rule formula returns true when the data is invalid. Most real rules need several conditions at once, so you will combine them with logical functions.
The core logical functions
Salesforce formulas use functions rather than infix keywords for combining conditions. You can also use &&, ||, and !, but the function forms are easier to read when nested.
| Function | Returns true when | Operator form | Column | Column |
|---|---|---|---|---|
| `AND(a, b)` | Both a and b are true | `a && b` | ||
| `OR(a, b)` | At least one is true | `a \ | \ | b` |
| `NOT(a)` | a is false | `!a` | ||
| `IF(test, x, y)` | Returns x if test is true, otherwise y | none |
AND and OR accept many arguments. AND(a, b, c) is true only if all three are true.
Building a rule step by step
Suppose a rule should block an Opportunity in the Negotiation/Review stage when Amount is missing or not positive. Build it from two parts.
- The stage condition:
ISPICKVAL(StageName, "Negotiation/Review") - The amount problem:
OR(ISBLANK(Amount), Amount <= 0)
Both must hold for the data to be invalid, so join them with AND:
AND(
ISPICKVAL(StageName, "Negotiation/Review"),
OR(ISBLANK(Amount), Amount <= 0)
)The AND says the record is in the stage the rule cares about. The OR says either problem is enough to fail. Mixing them up is the most common mistake. If you swapped them, the rule would fire on any record with a bad amount, whatever its stage.
Why the order of arguments can matter
Formulas do not reliably short-circuit. Do not count on an early FALSE in an AND to protect a later expression from an error. For example, dividing by a field that may be zero can still raise an error. Use IF to guard it:
IF(Quantity__c = 0, false, Total__c / Quantity__c > 500)Here the IF returns false for a zero quantity, so the division is only evaluated in the branch where it is safe.
Using IF inside a rule
You rarely need IF for simple rules, because AND and OR already return true or false. IF earns its place when the test itself changes by case:
IF(
ISPICKVAL(Type, "Customer"),
ISBLANK(Phone),
ISBLANK(Email)
)This rule fails a Customer that has no phone, and any other type that has no email.
Common errors
- Writing
ANDwhere you needOR, or the reverse. Read the formula aloud: "invalid when stage is X and amount is blank or not positive." - Forgetting parentheses around mixed
&&and||. UseANDandORfunctions to make grouping explicit. - Using
NOTon a whole group when one condition would do.NOT(AND(a, b))is equivalent toOR(NOT(a), NOT(b)), and the second form is often clearer for error messages. - Treating the formula as "what is valid". It must describe what is invalid.
Exercise
A rule should block saving an Account when Industry is "Banking" and either Phone or Website is blank. Write the error condition formula. Picklist: Industry.
Show answer
AND(ISPICKVAL(Industry, "Banking"), OR(ISBLANK(Phone), ISBLANK(Website)))
Exercise
Rewrite NOT(AND(Active__c, Amount > 0)) using OR and NOT on the individual conditions.
Show answer
OR(NOT(Active_c), NOT(Amount > 0)), which can also be written OR(NOT(Active_c), Amount <= 0). This follows De Morgan's law.