Sub topic 3
Writing the error condition formula
This chapter assumes you can open Setup and the Object Manager, know what objects, fields, and records are, and can read a spreadsheet-style formula. Here you will learn how to turn a business rule into the formula that a validation rule evaluates.
The formula describes bad data
A validation rule formula is an error condition. It returns a Boolean, which is a value that is either true or false. When the formula returns true, Salesforce blocks the save and shows the error message. When it returns false, the save continues.
This is the opposite of how most people state a business rule. People say "an Opportunity in Negotiation must have an Amount." The formula must describe the failure: "the stage is Negotiation and the Amount is missing."
The approach has three steps:
- Write the business rule in a plain sentence.
- Rewrite it as the situation you want to block.
- Translate each part of that sentence into a field test, and join the tests.
Worked example: Amount required in Negotiation
Business rule: an Opportunity in the Negotiation/Review stage needs a positive Amount.
The situation to block: the stage is Negotiation/Review, and the Amount is blank or not greater than zero.
AND(
ISPICKVAL(StageName, "Negotiation/Review"),
OR(ISBLANK(Amount), Amount <= 0)
)Each piece does one job:
ISPICKVAL(StageName, "Negotiation/Review")is true when the picklist holds that value. Picklists need this function because you can't compare them with=.ISBLANK(Amount)is true when the field has no value.Amount <= 0catches zero and negative numbers.OR(...)is true if either Amount problem exists.AND(...)requires the stage test and the Amount problem to both be true.
If the stage is Prospecting, the first test is false, so AND is false and the save goes through. The Amount tests never matter outside the stage you care about.
Guarding a test that can fail
Some tests only make sense when a field has a value. The Account example below checks a US postal code, but it skips the check when the field is empty:
AND(
BillingCountry = "USA",
NOT(ISBLANK(BillingPostalCode)),
NOT(REGEX(BillingPostalCode, "\\d{5}(-\\d{4})?"))
)Read it as a list of conditions that must all hold for the data to be bad: the country is USA, a postal code was entered, and that code does not match the pattern. Without the NOT(ISBLANK(...)) line, a blank code would also trigger the error, which blocks records the rule was never meant to check.
How the pieces fit
Common mistakes
- Writing the rule the right way round. If your formula describes valid data, every valid record gets blocked. Check by asking, "When this returns true, is the data bad?"
- Comparing a picklist with
=. UseISPICKVAL, orTEXT(field) = "value". - Forgetting blanks. A comparison such as
Amount <= 0does not reliably catch a missing value, so test blanks explicitly withISBLANK. - Over-broad scope. A rule with no scope condition runs on every save, including data loads and integrations.
Exercise
A business rule says "A Case with Status Closed must have a Resolution Notes value." Resolution Notes is a text field named ResolutionNotes_c, and Status is a picklist. Write the error condition formula.
Show answer
AND(ISPICKVAL(Status, "Closed"), ISBLANK(ResolutionNotes_c)). The formula describes the failure: the case is closed and the notes are missing. It returns true only for bad data, so the save is blocked only then.