You are currently viewing Best Practices for Validation Rules in Salesforce

Best Practices for Validation Rules in Salesforce

In today’s data-driven age, Data Quality Management plays an important role to ensure that the data used in an organization is accurate, reliable, and consistent. Maintaining the quality of data increases the accuracy of analytics applications that can lead to better business decision-making that increases sales, improves internal processes, and drives organizational performance.

For this, Salesforce, the global leader in CRM, provides various effective tools to improve the quality of your data. Validation Rules in Salesforce is one such feature that prevents users from entering incorrect data to maintain data quality and integrity. 

What are the Validation Rules?

Validation rules verify that the data a user enters in a record meets the standards you specify before the user can save the record. 

A validation rule can contain a formula or expression that evaluates the data in one or more fields and returns a value of “True” or “False”. Validation rules also include an error message to display to the user when the rule returns a value of “True” due to an invalid value.

After the validation rules are defined:

  • The user opts to edit a stored record or create a new one.
  • The user selects and clicks Save.
  • Verification of validation rules is completed.
  • Where all the data is valid, the record is saved.
  • Where there is invalid data, an error message is displayed and the record is not saved.
  • The necessary changes are made by the user who then selects Save again.

Components of Salesforce Validation Rules

Formula —  Formula is used to evaluate the business rule. If formula goes TRUE, a validation error is thrown otherwise it’s considered as a validation success.

Error message — Error message to be displayed in the event of a validation failure.

Location — Enables admins/developers to define the location to display validation error. In standard page layouts, it will allow the user to show error either at page top or below the selected field.

Calling – Validation rules are invoked anytime a record is created/updated via any process, i.e., manually from UI, API, etc.

Uses of Validation Rules

  • The validation rules are normally run when the user saves data.
  • If the validation rule contains an evaluation expression or formula then it may return True or False values. The error message is usually displayed at the top of the page or below the field that is associated with the error message.
  • When the rules are being written for the fields or the set of the fields then anyone of the below-listed action gets executed when you will click on the Save button.
  • Salesforce executes the validation rules that are specified by the user and if data is found validated then the record is saved, else it is not being saved.
  • If invalid data is being entered by the user then an error message will be displayed.
  • In case if the field that is associated with the validation rule is not displayed in the page layout, the validation rule will still be applied. If the rule fails in validation then an error message will be displayed on the screen.

    Best practices for Validation Rules in Salesforce

    • Never create validation rules directly in the Production environment.
    • Notify users whenever there are new rules.
    • Do not create multiple rules at once.
    • Never use specific IDs in a validation rule.
    • Run limited rules in Salesforce for the desired result.
    • You should always test your validation rule to ensure it works the way you intended.
    • Validation rules are applied to all fields of the record, irrespective of any page layout.

Steps to Create a Validation Rule
1.From Setup, go to Object Manager and click Account.
2.In the left sidebar, click Validation Rules.
3.Click New.
4.Enter the following properties for your validation rule:

  • Rule Name: Account_Number_8_Characters
  • Error Condition Formula:LEN( AccountNumber ) != 8

5.Error Message: Account number must be 8 characters long.
6. To check your formula for errors, click Check Syntax.
7. Click Save to finish.

Account Address Validation:–

 

Description:-   Validates that the account Billing Zip/Postal Code is in the correct format if Billing Country is Canada

Formula:-  AND(

OR(BillingCountry = “CAN”, BillingCountry = “CA”, BillingCountry = “Canada”),

NOT(REGEX(BillingPostalCode, “((?i)[ABCEGHJKLMNPRSTVXY]\\d[A-Z]?\\s?\\d[A-Z]\\d)?”))

)

.

Error Message:-  Canadian postal code must be in A9A 9A9 format.

Error Location:-   Billing Zip/Postal Code

Conclusion

Maintaining clean and accurate data is one of the most crucial things you can do to make the most out of Salesforce CRM. Validation rules in Salesforce ensure data accuracy and consistency by verifying that the data a user enters in your app meets the standards that you are specifying. If it doesn’t, then the validation rule prevents the record from being saved, and it will display an error message to the user.

Suraj

Suraj Tripathi

Salesforce Consultant | Solution Engineering Head

“Suraj Tripathi, a certified Salesforce Principal Consultant of repute, is a wonderful mentor and leader. A certified Salesforce Architect and a 7X Salesforce Certified Platform Application Developer by passion and profession, Suraj has rich experience in languages such as Aura, HTML, Angular, Bootstrap, APEX, and JavaScript. With more than five years of expertise in Salesforce Development, Suraj has worked on more than 50+ projects out of which 20+ projects were related to Salesforce Integration, Writing Triggers, Batch classes, VisualForce pages, and Aura Components. At Cloud Analogy, he is the backbone of a wide range of projects from all perspectives – technology, architecture, project management, customer stakeholder management, etc. His energy on the floor is difficult to match and his passion to accept nothing but excellence makes him a leader around whom a team can always rally.”

Leave a Reply