In this article, we will discuss how to create a validation rule in Salesforce. Validation rules are a powerful tool in Salesforce that can help ensure data quality and consistency. By setting up validation rules, you can enforce certain business requirements and prevent users from entering incorrect or incomplete data. In this tutorial, we’ll walk through the steps to create a validation rule in Salesforce. Also, we’ll provide some examples of when and how to use them.
What is a validation rule in salesforce
In Salesforce, a validation rule is a rule that you can define and apply to a custom object, standard object, or specific field in order to ensure that the data entered by users meets certain criteria. The rule can be set up to prevent users from entering incorrect or incomplete data into the system, which can help to maintain data integrity and accuracy.
When a user tries to save a record that violates a validation rule, Salesforce will display an error message indicating which rule was violated and what needs to be corrected in order to proceed with the save. Validation rules are typically used to enforce business rules or policies, such as ensuring that certain fields are filled out, or that data entered meets certain criteria (such as allowed range values, or a specific format).
You can create, manage, and define formula syntax (similar to that used in Salesforce workflows or process builder) of validation rules through the Salesforce user interface.
Steps to create a validation rule in Salesforce
- Log in to your Salesforce account as an administrator.
- Click on the gear icon in the top right corner of the screen, then select “Setup”.
- In the left sidebar, under “Platform Tools”, click on “Object Manager”.
- Select the object you want to create the validation rule for (accounts, contracts, opportunities, …)
- Click on “Validation Rules” in the left sidebar.
- Click on the “New” button to create a new validation rule.
- Enter a name and description for your validation rule.
- Define the criteria for your validation rule using a formula. For example, you could create a validation rule to prevent the creation of an opportunity if it does not have an amount value at specific stage.
- Check the syntax of your formula by clicking on the corresponding button under the formula field.
- Click “Save” to save your validation rule.
- Test your validation rule to make sure it is working as intended. Try creating a new record that violates the rule to make sure you get the error message.
- Adjust the error message as needed to be clear to users.
- Activate the validation rule.
Examples of validation rule formulas
Example 1 : check empty field if opportunity reach a specific stage
In this example, I want to check for example that users fill the description field when the opportunity reaches a specific stage.
For this, at the level of the formula, I use the “AND” operator with the step concerned as well as all the following steps and the verification that the field is not empty.
AND(OR(ISPICKVAL(StageName, "Stage1"), ISPICKVAL(StageName, "Stage2"), ISPICKVAL(StageName, "Stage3")), ISBLANK(Description))
Example 2 : Not allowing field edit if opportunity reaches a specific stage
In this example, I need to ensure that users cannot modify field1 from a certain stage onwards. To achieve this, I use the “OR” operator to specify the list of relevant stages and combine it with the “AND” operator in conjunction with the ISCHANGED function.
AND(OR(ISPICKVAL(StageName, "stage1"), ISPICKVAL(StageName, "Stage2"), ISPICKVAL(StageName, "Stage3")), ISCHANGED(field1))
Learn more about Salesforce validation rules
Here are two links to help you learn more about how to create a validation rule in Salesforce: