Sub topic 1
What validation rules do and when they fire
Prerequisites
This chapter assumes you know the basic Salesforce data model: objects, fields, and records. You should be able to find an object in Setup and the Object Manager. You should also be comfortable reading spreadsheet-style formulas such as AND(A1 > 0, B1 = "x"). No Apex is needed here.
What a validation rule does
A validation rule checks data at the moment a record is saved. If the data breaks a business rule, Salesforce stops the save and shows an error message. Nothing is written to the database.
Each rule has a formula called the error condition. The formula returns true or false.
- True means the data is invalid, so the save is blocked.
- False means the data passes, so the save continues.
This is the opposite of what many people expect. You write the formula to describe the bad case, not the good case.
When rules fire
A rule runs when a user or process tries to create or update a record. That includes:
- Saving a record in the Salesforce UI.
- Changes through the API, such as the Data Loader or an integration.
- Apex DML, such as
insertandupdate. - Record changes made by flows.
Rules do not run when a record is read or deleted. They also do not run on existing records that nobody edits. A record that was valid last year can become invalid under a new rule, and it will stay that way until someone tries to save it.
Validation runs after Salesforce loads the new values and before it commits them. The last chapter section of this tutorial covers the exact position in the order of execution. For now, remember that the rule sees the values that are about to be saved.
A first example
This rule blocks an Opportunity in the Negotiation/Review stage when Amount is blank or not positive.
AND(
ISPICKVAL(StageName, "Negotiation/Review"),
OR(ISBLANK(Amount), Amount <= 0)
)The formula is true only when both conditions hold. The stage must be Negotiation/Review, and Amount must be missing or zero or less. In every other case the formula is false and the save goes through.
Try these cases:
| Stage | Amount | Formula result | Outcome |
|---|---|---|---|
| Prospecting | blank | false | Saved |
| Negotiation/Review | 5000 | false | Saved |
| Negotiation/Review | blank | true | Blocked |
| Negotiation/Review | 0 | true | Blocked |
Common mistakes at this stage
- Writing the formula for the valid case. If your rule blocks good data, invert the logic.
- Assuming a new rule cleans old data. It only affects records that are saved afterward.
- Forgetting that integrations and data loads are also blocked. Later chapters show how to bypass rules with custom permissions.
Exercise
A rule's formula is ISBLANK(Phone). A user saves a Contact that has no phone number. Is the save allowed or blocked, and why?
Show answer
Blocked. The formula returns true for a blank Phone, and true means the data is invalid. The rule would force every Contact to have a phone number.
Exercise
You activate a new rule today. An existing record already breaks it, and nobody edits that record. What happens to it?
Show answer
Nothing. Rules fire only when a record is created or updated, so the record stays as it is until someone tries to save it.