The main purpose of using the Salesforce Validation rule verifies that the data a user enters in a record meets the standards you specify before the user can save the record. In case it doesn’t, the validation rule automatically shows an error message with a short explanation of what was entered incorrectly, this restricts the user to prevent from saving changes with invalid data. By having such rules, you can avoid incorrect values in the field.
Usually, these come in the form of required field sets that need to fill out with a particular field change.
How Do Validation Rules Work?
Validation Rules evaluate or trigger every single time there is an attempt to save the record for a particular field for which the validation rule has been written. The formulas contain one or more criteria that should be met in order to pass validation and save the record.
Simply put, the validation rules check whether a specific field (or multiple fields) corresponds to the mentioned criteria. If the data is evaluated to be true, the record gets saved. If not, the rule displays an error message, specifying the mistakes. This means that unless the user modifies the fields to match the criteria, they won’t be able to go through verification when a new record is created or when changes to an existing one are trying to be saved.
Don't forget to check out: Defining Validation Rules in Salesforce
Steps To Create Validation Rules
- Select your Object (Standard/ Custom). Go to the “Setup” section, from the Gear icon, then Object Manager from the tab, and choose your object from the detailed object list for which you want to create validation rules.
- Once you select your object, the object menu preview pane opens up, scroll down to the last and click on Validation Rule.
- Once the Validation Rule page opens up Click on New, and it will open the Object Validation Rule page.
- Name the Rule, Tick the “Active” checkbox, and in the “Description” field give a brief explanation of what the rule should do.
- Creating the Error Condition Formula. Moving on to the “Error Condition Formula Section”, we all need to create a formula that will identify the validation rule criteria. It’ll be made up of the chosen “Function” (on the right, you can see the box, containing a large selection of function categories), the selected field (via the “Insert Field”), and the operator (that’s indicated via the “Insert Operator” button).
- Once you specify your Logic for Validating Field in Error Condition formula Section by using various Functions sections, Insert operator Section (for various Arithmetic tools). Click on check syntax.
- The “Check syntax” Button will help you to evaluate your code logic and specify your “No Error Syntax” or “Error Syntax with Description” by evaluating your Formula.
- Defining the Error Message & its Location, The Error Message is a mandatory field when setting up a validation rule. Importantly, this message needs to state what was done wrong, i.e., where the user made a mistake, explaining in short how to make the fix.
- Specify the Error location, Mention the location of your Error message by choosing the radio button (either on the top of the page or beside the field for which the validation rule is made).
Check out another amazing blog by Saurabh here: Introduction to the Scratch Org | All You Need to Know
Finally, click on “Save” button to save the rule. Test the rule by creating a record and check whether the “Validation Rule” work as expected.