Sub topic 13
Where validation rules sit in the order of execution
This chapter assumes you can write a basic validation rule formula and have read simple Apex triggers or flows. You will learn which automation runs before and after a validation rule, and how that affects the values the rule sees.
The order of execution
When a record is saved, Salesforce runs its automation in a fixed sequence called the order of execution. Validation rules are one step in that sequence. A simplified version for a record save looks like this:
- Load the existing record, or initialize a new one.
- Overwrite the record with the new values from the request.
- Run system validation (required fields, field formats, maximum lengths). This step applies mainly to saves from the user interface.
- Run before-save record-triggered flows.
- Run before triggers.
- Run custom validation rules.
- Run duplicate rules.
- Save the record to the database without committing it.
- Run after triggers.
- Run assignment, auto-response, workflow, and escalation rules.
- Run after-save record-triggered flows.
- Commit the transaction.
Why the position matters
Before-save flows and before triggers run before your rules. A rule therefore evaluates the record as that automation left it, not as the user typed it.
The trigger below fills in a missing Amount before validation runs.
trigger OpportunityDefaults on Opportunity (before insert, before update) {
for (Opportunity opp : Trigger.new) {
if (opp.StageName == 'Negotiation/Review' && opp.Amount == null) {
opp.Amount = 1;
}
}
}With this trigger active, the rule from earlier chapters never fires for a blank Amount, because the field is no longer blank when the rule runs. This is a common reason a rule seems to be ignored.
The opposite is also true. A rule cannot see changes made by after triggers, after-save flows, or workflow field updates, because those run after the rule has already passed.
Failures after validation
Passing validation does not guarantee the save succeeds. After triggers, after-save flows, and other automation can still throw an error. When that happens, Salesforce rolls back the whole transaction, including the database write. Nothing is committed, so the user sees the error and the record is not saved.
Common mistakes
- Expecting a rule to catch a value that a before trigger or before-save flow has already corrected.
- Expecting a rule to check a value set by an after-save flow. The rule has already run by then.
- Debugging only the rule formula when the real cause is automation that changes the field earlier in the sequence.
- Assuming a failed rule has partially saved data. A blocked save writes nothing.
Exercise
A validation rule requires Amount to be greater than zero when the stage is Negotiation/Review. A before trigger sets Amount to 1 whenever it is blank. A user saves an Opportunity in that stage with no Amount. Does the rule block the save, and why?
Show answer
No. The before trigger runs before custom validation rules, so Amount is already 1 when the rule evaluates. The condition is false and the save continues. To enforce the rule, remove or change the trigger logic.