Create Validation Rule in Salesforce

I worked as a Salesforce admin for an organization that uses Salesforce CRM, and our sales team created opportunity records for converted leads.

While creating opportunity records, they had to restrict the close date field so that it would not be less than today’s date. Whenever the sales rep enters the close date today or earlier, an error message should display, and the record should not get saved.

To fulfill that requirement, we have a validation rule in Salesforce. Here, I will explain the validation rules in Salesforce, and then I will show you how to create a validation rule and apply it to different fields in Salesforce.

Validation Rule in Salesforce

In Salesforce, the validation rule requires the user to enter the correct data to save the records. If the user enters invalid input based on certain criteria, an error message will display on the screen.

Using the validation rule successfully gives quality data. The validation rule is evaluated regardless of the medium from which you create the records.

Below are some examples of applying validation rules on fields in Salesforce.

Validation Rule on Text Field in Salesforce

Here, we will take the Text Field example to display an error message when the text field contains a Blank Value or a Numeric Value.

  1. Go to the Gear Icon. -> Click on the Setup. -> Select the Object Manager tab.
Validation rule salesforce
  1. Select the Object on which you want to create the Validation Rule. Here, I’m going to create an Account Object.
Salesforce validation rule
  1. Now select the Validation Rules from the Account object.
    Salesforce validation rule for text field
    1. Now select the New button to create the new validation rule.
    Add validation rule for text field in salesforce
    1. Now, enter the Rule Name and check the Active checkbox to apply the rule. Then, enter the logic for Error Conditions in the Formula Editor.
    ISBLANK( Site ) || 
    NOT(REGEX( Site, "^[^0-9]*$"))
    • ISBLANK(): This function checks if the field is blank.
    • NOT(REGEX( Site, “^[^0-9]*$”)): This regular expression checks that the field does not contain a numeric value.
    • Site: This is the Account Field; we can insert it from the Insert Field button.

    Now, to check whether the entered code is correct or not, click on the Check Syntax button.

    How to Create Validation Rule in Salesforce
    1. Error Message: Now, enter a message to display an error when the user enters incorrect details. Error Location: Where the error message should display on the screen. For that, we have the following two options.
      • Top of Page: The error message will display beside the Save button.
      • Field: The error message will display on a particular field for which we apply validation.
      • Then click on the Save button.
    Apply Validation Rule on Text Field in Salesforce
    1. Now, navigate to the Account Record Detail Page to create a record and enter the incorrect detail in the field where we created the validation rule.
      • Here, you can see that the validation rule has been successfully applied, and an error message is displayed.
    Create Validation for text Field in Salesforce

    In this way, we can add a validation rule for the text field in Salesforce.

    Validation Rule on Number Field in Salesforce

    Here, we will take the Number Field example to display an error message when the user enters out-of-range numbers. For example, we have the Rating_0_5__c field on an Account, which ranges from 0 to 5.

    1. Follow the above step to navigate to the Object and Validation Rule option. Here also, I’m creating a rule on the Account object.
      • Now, enter the Rule Name and check the Active checkbox to apply the rule. Then, enter the logic for Error Conditions in the Formula Editor.
    OR( Ratings_0_5__c  < 0,  Ratings_0_5__c  > 5)
    • This formula will display an error if the user does not enter a value in the Number Field within the specified range (0-5).
    Apply Validation Rule on Number Field in Salesforce
    1. Now enter the Error Message and the Error Location. Here, I have selected the ‘ Field as the error location of the ‘Rating 0-5’ Field. Then click on the Save validation rule.
    Validation Rule on Number Field in Salesforce
    1. Here, you can see that if the user enters a number out of range, it displays an error message.
    Create Validation Rule on Number Field in Salesforce

    This is how to add a validation rule for the Number Field in Salesforce.

    Validation Rule on Date Field in Salesforce

    Here, we will take the Date Field example to display an error message when the user selects the date field Today or Less than Today.

    1. Follow the above step to navigate to the Object and Validation Rule option. I’m going to create a validation rule for the Close Date of Opportunity object.
      • Now, enter the Rule Name and check the Active checkbox to apply the rule. Then, enter the logic for Error Conditions in the Formula Editor.
    CloseDate <= TODAY()
    • Today(): This function returns the Current Date.
    • CloseDate: This is the Opportunity Field; we can insert it from the Insert Field button.

    This formula will display an error when the user selects a close date that is today or earlier.

    Validation Rule on Date Field in Salesforce
    1. Now, enter the Error Message that will display on the Record Detail Page and select the error message location. Here, I have selected the error location in the Close Date Field.
      • Then click on the Save button.
    Apply Validation Rule on Date Field in Salesforce
    1. Today’s date is 20/05/2024. However, when the user selects a Close Date less than Today, an error message is displayed in the close date field.
    Create Validation Rule on Date Field in Salesforce

    This is how to add a validation rule for the Date Field in Salesforce.

    Conclusion

    I hope you now have an idea about validation rules in Salesforce. We have seen what validation is in Salesforce and how to create and apply validation rules to different Fields in Salesforce.

    I explained the validation rule by applying it to the text, number, and date fields. So, using a validation rule, we can restrict the user from entering incorrect data in Salesforce.

    You may like to read:

    Agentforce in Salesforce

    DOWNLOAD FREE AGENTFORCE EBOOK

    Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

    Salesforce flows complete guide

    FREE SALESFORCE FLOW EBOOK

    Learn how to work with flows in Salesforce with 5 different real time examples.