How do Validation Rules enforce data quality standards and display custom error messages to users?
Answer
Basics: Required fields and picklists cannot express conditional business rules such as "Close Date cannot be in the past" or "Discount over 20% needs a reason". Validation rules express those. They run on create and update (not on delete) whether the record is saved by UI, API, Flow, Apex or data import.
How it works:
- Setup > Object Manager > object > Validation Rules > New.
- Write the Error Condition Formula (TRUE means error).
- Provide an Error Message and choose Error Location: Top of Page or a specific field.
- Activate. Use Check Syntax first.
- In the order of execution, custom validation runs after system validation and before-save flows/before triggers have run, and again fires on final values; the rule sees the values after before triggers and before-save flows have updated them.
Advanced / deep dive: Useful functions: ISBLANK, ISCHANGED, ISNEW, PRIORVALUE, REGEX, AND/OR/NOT, CASE, cross-object fields through lookups, $User, $Profile, $Permission and $Setup (custom metadata/custom settings) to implement bypass switches. Use ISBLANK rather than ISNULL for text and numbers (ISNULL is only reliable for dates in some contexts). A bypass via $Permission.Bypass_Validation is better than profile names, which are brittle. Validation rules can block integrations and data loads, so scope them with $User checks or custom-permission bypass. They do not run on records deleted, nor on rollup changes that do not modify the child directly; a rule may also fire when a Flow or trigger updates the record for unrelated reasons, which can surprise admins. For complex logic that is hard to read, use Apex or a Flow with clear errors; for cross-record checks, validation rules only see parent fields through lookups, not child collections. Formula size limits apply (character and compiled-size limits); keep rules short.