What are Validation Rules in Salesforce?
Validation rules consist of standards to guarantee that the data entered into Salesforce meets specific
criteria before it gets saved. There are two main purposes of Validations Rules in Salesforce – a) to
prevent users from entering inaccurate or insufficient information. b) to maintain data consistency across
various fields and objects.
There are two main types of validation rules:
System Validation Rules: Salesforce has pre-built rules like “preventing future dates for
historical records” that maintain fundamental data consistency.
Custom Validation Rules: These are created by administrators to address your organization’s
unique needs. Formulas and expressions can be defined to validate data in different fields.
How to Create Validation Rules?
1. Click on the Object Manager to find the object in which you want to create the validation rules.
2. In the detail page of that object, there is a left sidebar to find the Validation Rules tab.
3. Click on the “Validation Rule”.
4. Click on the “New” button.
5. Specify the conditions that the validation rule must meet.
6. Give the error message that will appear when the requirements are not satisfied.
7. Click on the “Save” button.
Scenarios of Salesforce Validation Rules –
1. Validates that the phone number begins with the country code, which is represented by the plus
sign (+).
Object – ‘Account’ and Field – ‘Phone’
Formula –
LEFT(Phone, 1) <> “+”
2. Validates if a custom discount percentage field falls within the range of 0 to 40%.
Object – ‘Opportunity’ and Field – ‘Discount Rate’
Formula –
‘OR(Discount_Rate__c < 0, Discount_Rate__c > 0.40).’
3. Validates that only the owner of a record can edit a custom field named Mailing Address.
Object – ‘Contact’ and Field – ‘MailingAddress’
Formula –
‘AND ( ISCHANGED ( MailingAddress ), Owner <> $User.Id) .’
4. Validates that the Account Number is numeric if not blank.
Object – ‘Account’ and Field – ‘AccountNumber__c’.
Formula –
‘OR (ISBLANK(AccountNumber),NOT(ISNUMBER(AccountNumber)))’
5. Make the required field for the lead generation.
Object – ‘Lead’, Field – ‘Email’, ‘Phone’, and ‘FirstName’
Formula –
AND(
ISBLANK(Email),
ISBLANK(Phone),
ISBLANK(FirstName)
)
6. Validate that the Account should be created through only qualified leads.
Object – ‘Lead’ and Field – ‘Status’
Formula –
AND(
IsConverted,
NOT(ISPICKVAL(Status, ‘Qualified’))
)
7. Prevents users from setting a close date for an opportunity in the past.
Object – ‘Opportunity’ and Field – ‘CloseDate’
Formula-
‘CloseDate < TODAY()’
8. Validates that the email address entered follows a proper email format.
Object – ‘Contact’ and Field- ‘Email’
Formula –
‘NOT(ISBLANK(Email)) && NOT(REGEX(Email, “[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}”))’
9. If a case is already open, prevent the Status from being changed back to “New”.
Object – ‘Case’ and Field – ‘Status’
Formula –
‘AND(
ISCHANGED( Status ),
NOT(ISPICKVAL(PRIORVALUE( Status ), “New”)),
ISPICKVAL( Status, “New”)
)‘
10. Validates that a custom field called Other Reason contains a value if a case has a Case Reason of
“Other.” Description of Other Reason is required.
Object – ‘Case’ and Field – ‘Reason’
Formula –
‘AND(
ISPICKVAL( Reason, “Other” ),
ISBLANK(Other_Reason__c)
)‘
11. Prevents users from changing the owner of a record to themselves.
Object – Custom Object (Attendance Record)
Formula –
‘Owner:User.Id = $User.Id && ISCHANGED(OwnerId)’
12. Validates that the value of a custom date field is a weekday (not Saturday or Sunday).
Object – ‘AttendanceRecord__c’ and Field – My_Attendance_Date__c
Formula –
‘CASE(
MOD(
My_Attendance_Date__c – DATE(1900, 1, 7),
7
),
0, 0,
6, 0,
1
) = 0′
13. To ensure that a contact in Salesforce has at least one communication channel (phone or email).
Object – ‘Contact’ and Field – ‘Email’ and ‘Phone’
Formula –
‘AND(
ISBLANK(Email),
ISBLANK(Phone)
)’
14. Dependent Field Validation: Requires users to enter a credit card number if they select “Credit
Card” as the payment method.
Object – Car Detail__c Field – Payment_Method__c
Formula –
‘ISPICKVAL( Payment_Method__c , “Credit Card”) && ISBLANK( Credit_card_number__c )’
15. Consider a scenario where a business rule requires that if an Opportunity is marked as ‘Closed
Won’, the ‘Amount’ field must be greater than zero.
Object – ‘Opportunity’ and Field – ‘StageName’
Formula –
‘AND(
ISPICKVAL(StageName, “Closed Won”),
Amount <= 0
)’
16. To prevent the creation of cases of Inactive Accounts.
Object – ‘Case’ and Field – ‘Account.Active__c ‘
Formula –
‘AND(
ISNEW(),
NOT(ISBLANK( AccountId)),
ISPICKVAL( Account.Active__c , ‘NO’)
)’
17. Restricting Field Updates Based on User Profile- To restrict specific fields from being updated by
users with the “Marketing User” profile after the opportunity stage is set to “Closed Won.”
Object – ‘Opportunity’ and Field – ‘Amount’
Formula –
‘AND(
ISCHANGED(Amount),
ISPICKVAL(StageName, “Closed Won”),
$Profile.Name= “Marketing User”
)’
18. Validate whether the Student’s age is more than 18 years or not. This will be through an error
when the student’s age is less than 18.
Object – ‘AttendanceRecord__c’ and Field – ‘Age__c’
Formula –
‘Age__c < 18’
19. Validate that the Mobile number should not exceed 10 digits.
Object – ‘AttendanceRecord__c’ and Field – ‘mobile_c’
Formula –
‘LEN(Attendance__c.mobile_c) > 10’
20. Validate that the picklist value should not be “None”.
Object – Student__c and Field – Degree__c
Formula –
ISPICKVAL(Degree__c, “None”)
1. The account number is Numeric
There might be a scenario where you want your user to enter only numeric values in the Account
Number field of Account Object. You can configure the below validation rule for the same.
OR(
ISBLANK(AccountNumber),
NOT(ISNUMBER(AccountNumber))
Below is the screenshot of the validation rule.
Now if you try to enter text value in the Account Number field it will give an error message as you can
see below.
2. Annual Revenue Range
There could be scenarios where you don’t deal with accounts having annual revenue greater than a
specific number. You can enforce this using the below rule.
OR(
AnnualRevenue < 0,
AnnualRevenue > 1000000
Below is the screenshot of the rule.
Now if you try to enter annual revenue greater than 1 million then you will get an error.
3. Close Date Must Be a Future Date
Sales reps often select past dates in an opportunity closed date field and you want to restrict them.
This can be achieved by the below validation rule.
CloseDate< today()
Below is the screenshot of the rule.
Below is the error you will get when you enter the past date in the opportunity close date field.
4. Prevent Open Cases from Being Reset to New
Sometimes service agents change the status of open cases to New you want to restrict. These can be
achieved by below validation rules.
AND(
ISCHANGED( Status ),
NOT(ISPICKVAL(PRIORVALUE( Status ), "New")),
ISPICKVAL( Status, "New")
Below is the screenshot of the configured validation rule.
And below is the screenshot of the error message which you will get while trying to set the status of
open cases to new.
5. Blank Email or Mobile
There can be scenarios where you want at least one communication detail to be available on contact
record like email or mobile number. These can be configured by the below validation rule.
AND(
ISBLANK( Email ) ,
ISBLANK( MobilePhone )
Below is the screenshot of the configured rule.
Below is the error you will get when you leave email or mobile empty.
Also, Learn more about Order of execution in Salesforce
FAQs
1. What are the best practices of the Validations Rules?
Error messages should clearly explain exactly why the rule was triggered as well as how to
correct the mistake.
When presenting error messages, make sure the error code is distinct. When debugging, this
code helps in locating the validation rule. . Use a naming convention to easily identify the object
and its specific validation rules.
To provide understandable error messages and identify exactly which field requires attention,
create separate rules depending on the need.
2. What is the difference between the formula field and the validation rule in Salesforce?
A formula field is a read-only field that takes its value from a user-defined formula or expression. In
contrast, Validation rules validate if the data entered by the user in records meets specified standards
before saving.
Conclusion
Validation Rules are very important for making sure that the information in Salesforce is correct. They
help stop mistakes like missing data or wrong information from being entered. The examples show how
you can use them in different ways to check data.
Trailhead tutorial:
https://trailhead.salesforce.com/content/learn/modules/validation-rules