Sub topic 6
Custom validation rules after before triggers
This chapter assumes you can write a simple before trigger and a validation rule formula. It also assumes you know that a save is one transaction.
Where validation sits in the order
Validation runs twice in a save. The first pass happens early, before any before-save flow or trigger. The second pass happens after the before triggers finish. This chapter covers the second pass.
After the before triggers, Salesforce checks required fields and field formats again. It then evaluates your custom validation rules. Layout-specific checks, such as a field required only on a page layout, run only in the first pass.
Validation rules see the trigger's changes
A validation rule evaluates the record as it stands after the before triggers. If a before-save flow or trigger changed a value, the rule sees the new value, not what the user entered.
This trigger sets a rating from revenue:
trigger AccountRating on Account (before insert, before update) {
for (Account a : Trigger.new) {
if (a.AnnualRevenue != null && a.AnnualRevenue > 1000000) {
a.Rating = 'Hot';
}
}
}Now add this validation rule on Account, with the error message "Hot accounts need a phone number":
AND(ISPICKVAL(Rating, "Hot"), ISBLANK(Phone))A user saves an account with revenue of 2,000,000 and no phone. They never touched Rating. The trigger sets it to Hot, the rule then sees Hot with a blank Phone, and the save fails. The user gets an error about a value they did not enter.
Common errors
- Assuming the rule sees the raw input. Rules see trigger and flow output. When a rule fires unexpectedly, check what your before logic changed.
- Expecting Id in a rule on insert. The record has no Id yet during validation, so a rule that uses
Idon a new record behaves as if it is blank. - Expecting validation after an after trigger. Rules do not run again after the database save. An after trigger that changes a record through DML starts a new save for that record, and that save has its own validation pass.
- Mixing up the two ways to block a save.
addError()in a trigger and a validation rule both cancel the save. The error appears in a different step, and rules are easier for admins to maintain.
Choosing between a rule and addError
Use a validation rule when the check depends only on field values on the record. Use addError() in a before trigger when the check needs queries or comparisons with related records. Both block the save, but a rule always runs after the before-trigger changes, so write it with those changes in mind.
Exercise
A before insert trigger sets Status_c to 'Active' when it is blank. A validation rule blocks saves where Statusc is 'Active' and StartDate__c is blank. A user saves a record with both fields blank. Does the save succeed, and why?
Show answer
It fails. The before trigger sets Status_c to 'Active' first. The validation rule then runs on the changed record, sees 'Active' with a blank StartDate__c, and blocks the save. The transaction rolls back.