Sub topic 3
Writing the error condition formula
This chapter assumes you know the Salesforce data model (objects, fields, records), can open Setup and the Object Manager, and can read spreadsheet-style formulas. It covers the central part of a validation rule: the error condition formula.
The one rule: true means invalid
A validation rule has a formula that returns a Boolean (true or false). The logic runs opposite to what many new admins expect.
- If the formula returns true, the data is invalid. Salesforce blocks the save and shows the error message.
- If the formula returns false, the data is valid. The save continues.
So you do not write "what a good record looks like." You write "what a bad record looks like." Say the business rule is "An opportunity in Negotiation/Review must have a positive amount." The formula describes the violation: the stage is Negotiation/Review and the amount is missing or not positive.
AND(
ISPICKVAL(StageName, "Negotiation/Review"),
OR(ISBLANK(Amount), Amount <= 0)
)When a user saves an opportunity, Salesforce evaluates this formula against the record. For a Negotiation/Review opportunity with a blank Amount, both parts are true, so the formula returns true and the save is blocked. For a Prospecting opportunity, ISPICKVAL returns false, AND returns false, and the save goes through.
A method for writing the formula
Work in four steps.
- State the business rule in plain words. For example: "Closed Won deals need a close date that is not in the future."
- Invert it into a violation. Ask "When is this rule broken?" The answer is the error condition.
- Break the violation into small checks. Each check is one comparison or function call, such as a stage test or a blank test.
- Combine the checks with
AND,OR, andNOT, then test the result with sample records.
Step 2 is where most mistakes happen. Writing the valid condition and shipping it unchanged blocks every good record and allows every bad one. If you find a rule that behaves backwards, check whether the formula describes validity instead of violation.
Inverting with NOT
Sometimes the rule is easier to state as a requirement. You can write the requirement and wrap it in NOT. For "Billing country USA requires a five-digit ZIP or ZIP+4":
AND(
BillingCountry = "USA",
NOT(ISBLANK(BillingPostalCode)),
NOT(REGEX(BillingPostalCode, "\\d{5}(-\\d{4})?"))
)Here REGEX returns true for a well-formed code, so NOT(REGEX(...)) returns true for a malformed one. The middle line skips the check when the field is blank, so the rule does not fire on empty values. Later chapters cover REGEX and blank handling in detail. For now, notice the structure: a scope check, a precondition, then the violation.
Reading a formula from the inside out
Salesforce evaluates nested functions from the innermost call outward. To understand any formula, evaluate it the same way. Take the Opportunity example with these values: Stage is Negotiation/Review, Amount is blank.
| Step | Expression | Result |
|---|---|---|
| 1 | `ISPICKVAL(StageName, "Negotiation/Review")` | true |
| 2 | `ISBLANK(Amount)` | true |
| 3 | `Amount <= 0` | evaluates with a blank amount (see below) |
| 4 | `OR(step 2, step 3)` | true, because step 2 is true |
| 5 | `AND(step 1, step 4)` | true, so the save is blocked |
For a blank number, Salesforce treats the field as zero in a comparison when the formula is set to "Treat blank fields as zero" in older formula settings. In validation rules, blank numeric fields are handled as null in the check, which is why you test ISBLANK(Amount) explicitly instead of relying on Amount <= 0 to catch it. Testing blanks directly makes the intent clear and keeps the formula independent of that setting.
Operators you will use constantly
The error condition is built from a small set of operators. Each returns true or false when used in a comparison.
| Operator | Meaning | Example | Column | Column |
|---|---|---|---|---|
| `=` or `==` | Equal | `BillingCountry = "USA"` | ||
| `<>` or `!=` | Not equal | `Type <> "Customer"` | ||
| `<`, `>`, `<=`, `>=` | Numeric, date, or time comparison | `CloseDate < TODAY()` | ||
| `&&` or `AND()` | Both sides true | `AND(A, B)` | ||
| `\ | \ | ` or `OR()` | At least one side true | `OR(A, B)` |
| `!` or `NOT()` | Reverses the value | `NOT(ISBLANK(Phone))` |
Use the function forms AND, OR, and NOT when a formula grows past two conditions. They accept many arguments and the layout is easier to read than a chain of && and ||. Indent each argument on its own line, as in the examples above.
String comparison with = is not case-sensitive. "usa" equals "USA". If you need a case-sensitive test, use EXACT(text1, text2).
Fields that need special handling
Not every field type can be compared with = directly.
- Picklists cannot be used in a plain comparison. Use
ISPICKVAL(field, "Value")for one value, orTEXT(field)to turn the picklist into text for other functions. WriteISPICKVAL(StageName, "Closed Won"), notStageName = "Closed Won". - Blank checks use
ISBLANK(field)for most field types. For a text field that might contain only spaces,ISBLANKtreats whitespace-only values as blank in validation rules, but you should test with real data to confirm. - Checkboxes are Boolean already. Write
IsPrivateorNOT(IsPrivate)without comparing toTRUE. - Dates compare with
<and>againstTODAY()or another date. Do not mix a date and a date/time without converting one of them.
Where the formula is entered
In Setup, open the Object Manager, choose the object, then Validation Rules and New. You give the rule a name, tick Active, and paste the formula in the Error Condition Formula box. The Check Syntax button catches typos and mismatched parentheses, but it cannot tell you whether the logic is right. It only confirms the formula returns a Boolean.
The same box lets you insert fields, operators, and functions from lists. This helps you spell API names correctly. Always use API names, such as Custom_Field__c, not field labels.
Common mistakes
- Describing validity instead of violation. The rule blocks the wrong records. Re-read the formula and ask "when does this return true?"
- Forgetting a blank check. A field that is blank can make a comparison return false when you expected true, or fail the save when you expected it to pass. Add an explicit
ISBLANKtest where blank is a legal value. - Using
=on a picklist. Syntax check fails. UseISPICKVAL. - Missing parentheses or commas. Nested
ANDandORcalls make this common. Format each argument on its own line. - Firing on every edit. A rule with no scope check runs each time any field on the record is saved. That can block users who edit unrelated fields on old, dirty records. The change-detection chapter covers
ISNEWandISCHANGED. - Text in the wrong quotes. Use straight double quotes
"for string literals.
A worked example
The rule: "Cases cannot be closed unless a Resolution is entered." Suppose the Case object has a custom text field Resolution__c and the standard Status picklist.
- Business rule: closed cases need a resolution.
- Violation: the case is closed and the resolution is blank.
- Checks:
ISPICKVAL(Status, "Closed")andISBLANK(Resolution__c). - Combined:
AND(
ISPICKVAL(Status, "Closed"),
ISBLANK(Resolution__c)
)Walk through two records. A Closed case with a blank Resolution returns true and the save is blocked. A Working case with a blank Resolution returns false at the first check, so the save is allowed. A Closed case with text in Resolution returns false at the second check, so the save is allowed too.
Exercise
A rule must stop users from saving an Account when Type is "Customer" and the Phone field is blank. Write the error condition formula, and say what the formula returns for a Customer account that has a phone number.
Show answer
AND(ISPICKVAL(Type, "Customer"), ISBLANK(Phone)). Type is a picklist, so it needs ISPICKVAL. For a Customer account with a phone number, ISBLANK(Phone) is false, so AND returns false and the save is allowed.
Exercise
A teammate wrote the formula AND(ISPICKVAL(StageName, "Closed Won"), NOT(ISBLANK(Amount))) to enforce "Closed Won opportunities must have an amount." Users report that valid records are blocked and invalid ones save. What went wrong, and what is the fix?
Show answer
The formula describes the valid state instead of the violation. It returns true, and blocks the save, when the opportunity is Closed Won and has an amount. The fix is to flag the missing amount: AND(ISPICKVAL(StageName, "Closed Won"), ISBLANK(Amount)).