20 Important Validation Rules in Salesforce

20 Important Validation Rules in Salesforce

This blog explores how the Validation rules in Salesforce play a significant role, how to create them, and provides numerous examples to illustrate their use in various scenarios.

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) <> “+”

Validation Rules in Salesforce

Validation Rules in Salesforce

 

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).’

Validation Rules in Salesforce

 

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) .’
Validation Rules in Salesforce

 

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”)

 

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.

Get a complete Roadmap To Learn Salesforce Admin And Development

Share Now

Neelu is pursuing her career in the Salesforce domain. She believe that words are powerful tools, and she love using them to express herself, both in technical and creative ways. This passion for language makes her a bit of a logophile (a lover of words!). In her free time, she enjoy crafting quotes to share her thoughts and inspirations.

Similar Posts

One thought on “20 Important Validation Rules in Salesforce

Leave a Reply

Your email address will not be published. Required fields are marked *