Validation Rules
Learning Content
Last updated: July 21, 2026
Validation Rules
A Validation Rule is a declarative Salesforce feature that verifies whether a record meets predefined business requirements before it is saved. If the validation condition evaluates to TRUE, Salesforce prevents the record from being saved and displays an error message to the user.
Validation Rules help maintain data quality, enforce business policies, and ensure that only valid information is stored in the Salesforce databaseβall without writing Apex code.
π‘ LearnFrenzy Insight
Imagine submitting an online application where mandatory fields such as your email address or phone number are missing.
Instead of accepting incomplete information, the system immediately asks you to correct the mistakes.
A Validation Rule works in exactly the same wayβit stops invalid records before they are saved.
Where Do Validation Rules Execute?
User Clicks Save
β
βΌ
System Validation
β
βΌ
Before-Save Flow
β
βΌ
Before Trigger
β
βΌ
β Validation Rules
β
βΌ
Duplicate Rules
β
βΌ
Database Save
Validation Rules execute after the record has been prepared by Before-Save Flows and Before Triggers, but before Salesforce writes the record to the database. If any validation fails, the transaction stops and the record is not saved.
Why Use Validation Rules?
| Purpose | Benefit |
|---|---|
| Maintain Data Quality | Prevent incomplete or incorrect information from being stored. |
| Enforce Business Policies | Ensure users follow organizational rules. |
| Reduce Errors | Catch mistakes before the record is committed. |
| Declarative Automation | No Apex code required for most validation scenarios. |
| Consistent Data Entry | Apply the same validation rules across all users. |
Common Business Examples
| Requirement | Validation Rule |
|---|---|
| Close Date cannot be in the past | Prevent users from saving invalid Opportunity dates. |
| Email is mandatory | Require an email before saving a Contact. |
| Discount cannot exceed 30% | Enforce company pricing policy. |
| Annual Revenue must be positive | Reject negative values. |
| Stage cannot be Closed Won without Amount | Ensure important fields are completed. |
Simple Validation Rule Example
AND(
ISPICKVAL(StageName,"Closed Won"),
ISBLANK(Amount)
)
If the Opportunity Stage is Closed Won but the Amount field is blank, Salesforce displays an error message and prevents the record from being saved.
Real Business Scenario
π’ Business Example
A company requires every Opportunity marked as Closed Won to include:
- Opportunity Amount
- Expected Close Date
- Primary Contact
If any required information is missing, Salesforce stops the save operation and asks the sales representative to complete the record.
Validation Rule vs Before Trigger
| Feature | Validation Rule | Before Trigger |
|---|---|---|
| Requires Apex | No | Yes |
| Complex Logic | Limited | Excellent |
| Cross-Object Processing | Limited | Supported |
| Error Messages | Built-in | Uses addError() |
| Best For | Simple business validations | Advanced validations and processing |
When Should You Use Validation Rules?
| Use Validation Rules | Avoid Validation Rules |
|---|---|
| Mandatory fields | Complex calculations |
| Field value restrictions | External integrations |
| Business policy enforcement | Creating related records |
| Simple comparisons | Heavy Apex logic |
| Declarative validations | Bulk business processing |
π Behind the Scenes: What Happens Internally?
User Clicks Save
β
βΌ
Before-Save Flow Updates Record
β
βΌ
Before Trigger Executes
β
βΌ
Validation Rules Evaluate
β
βΌ
Any Rule Returns TRUE?
β
Yes β No
β
βΌ
Display Error
β
Transaction Stops
Validation Rules evaluate the record after earlier automation has modified its values. If the final state of the record violates any rule, Salesforce stops the transaction before the database save occurs.
β
Best Practice
Use Validation Rules for declarative business validations whenever possible. Keep formulas simple, write meaningful error messages, and avoid duplicating the same validation logic in Apex unless there is a specific technical requirement.
β οΈ Common Mistake
Using Validation Rules for scenarios that require related-record queries, complex calculations, or reusable business logic. Those cases are better handled with Apex or Flow.
π― Interview Tip
Question: What is the difference between a Validation Rule and addError() in a Before Trigger?
Answer: Validation Rules are declarative and ideal for straightforward business validations. addError() is used in Apex when validation depends on complex logic, reusable classes, or information from related records that cannot be handled easily with formulas.
π Quick Revision
- β Executes before the database save.
- β Runs after Before Trigger.
- β Stops invalid records from being saved.
- β Displays user-friendly error messages.
- β Best for declarative business validations.
- β Does not require Apex code.
β‘ Next Lesson
In the next lesson, you'll learn how Duplicate Rules detect potential duplicate records, how they fit into the Salesforce Order of Execution, and how they differ from Validation Rules.
Practice What You've Learned
Test your understanding with these practice exercises