Salesforce Validation Rules Examples: A Practical Guide
Salesforce validation rules ensure data quality and consistency by enforcing specific conditions before records are saved. These rules help prevent users from entering incomplete or incorrect information, maintaining the integrity of your Salesforce data.
This article provides examples of commonly used Salesforce validation rules and explains how to create and implement them effectively.
What Are Salesforce Validation Rules?
Validation rules evaluate data entered by users against predefined criteria. If the data does not meet the criteria, Salesforce prevents the record from being saved and displays an error message.
Components of a Validation Rule
- Formula: A logical expression that evaluates to
TRUE
when the rule is violated. - Error Message: A custom message displayed when the validation rule triggers.
- Error Location: Indicates where the error message should appear (field or page level).
Salesforce Validation Rule Examples
1. Ensure Mandatory Fields Are Populated
Scenario: Require a Phone
number on the Account
object if the Type
is Customer
.
Formula:
AND(
ISPICKVAL(Type, "Customer"),
ISBLANK(Phone)
)
Error Message:Phone is required for Customer accounts.
Error Location:
- Field:
Phone
.
2. Validate Email Format
Scenario: Ensure that email addresses follow a valid format.
Formula:
NOT(REGEX(Email, "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$"))
Error Message:Please enter a valid email address.
Error Location:
- Field:
Email
.
3. Prevent Close Date in the Past
Scenario: Prohibit setting a Close Date
on an Opportunity
earlier than today.
Formula:
CloseDate < TODAY()
Error Message:Close Date cannot be in the past.
Error Location:
- Field:
Close Date
.
4. Enforce Numeric Values in Custom Fields
Scenario: Ensure a custom text field Custom_Number__c
contains only numeric values.
Formula:
NOT(REGEX(Custom_Number__c, "^[0-9]*$"))
Error Message:Custom Number must contain only numeric values.
Error Location:
- Field:
Custom Number
.
5. Restrict Record Creation Based on Role
Scenario: Prevent users with the Sales Rep
role from creating Opportunities
over $1,000,000.
Formula:
AND(
$Profile.Name = "Sales Rep",
Amount > 1000000
)
Error Message:Sales Reps cannot create Opportunities over $1,000,000.
Error Location:
- Page level.
6. Ensure Start Date Is Before End Date
Scenario: Validate that the Start Date
of a project is earlier than the End Date
.
Formula:
Start_Date__c > End_Date__c
Error Message:Start Date must be earlier than End Date.
Error Location:
- Field:
Start Date
.
7. Limit Picklist Values
Scenario: Restrict Stage
on Opportunities
to certain values based on the Type
field.
Formula:
AND(
ISPICKVAL(Type, "New Business"),
NOT(
OR(
ISPICKVAL(StageName, "Prospecting"),
ISPICKVAL(StageName, "Qualification"),
ISPICKVAL(StageName, "Proposal/Quote")
)
)
)
Error Message:Stage must be Prospecting, Qualification, or Proposal/Quote for New Business opportunities.
Error Location:
- Field:
Stage
.
How to Create a Validation Rule in Salesforce
- Navigate to Object Manager:
- Go to Setup → Object Manager → Select the desired object (e.g.,
Account
).
- Go to Setup → Object Manager → Select the desired object (e.g.,
- Create a Validation Rule:
- Click Validation Rules → New.
- Define the Rule:
- Enter a Rule Name.
- Write the Error Condition Formula.
- Add a Error Message and specify the error location.
- Test the Rule:
- Save the rule and test it by attempting to create or update records that meet the criteria.
- Activate the Rule:
- Activate the rule to enforce it in your organization.
Best Practices for Validation Rules
- Write Clear Error Messages:
- Provide actionable error messages to help users correct their input.
- Test Thoroughly:
- Test rules in a sandbox environment before deploying them to production.
- Avoid Overlapping Rules:
- Ensure validation rules don’t conflict, which can confuse users or prevent data entry.
- Use Logical Operators:
- Combine conditions efficiently using
AND
,OR
, andNOT
to avoid redundant rules.
- Combine conditions efficiently using
- Document Rules:
- Maintain documentation for all validation rules for easier troubleshooting and updates.
Conclusion
Salesforce validation rules are essential for maintaining high-quality data and enforcing business logic. This guide covered common validation rule examples and practical scenarios, helping you implement rules effectively. By leveraging these examples, you can ensure data consistency and improve user experience in your Salesforce organization.