Validation Rules in Salesforce

In Salesforce, the Validation Rule restricts the user from entering the incorrect date before saving the record.

In this Salesforce tutorial, we will learn How to Apply Validation Rules in Salesforce. In that, we will see the following topics:

  • Validation Rule in Salesforce.
  • Validation Rule on Text Field in Salesforce.
  • Validation Rule on Number Field in Salesforce
  • Validation Rule on Date Field 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.

You can read the article on How to Bypass Validation Rules Using Salesforce Flow.

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 Numeric Value.

Step 1. Go to the Gear Icon. -> Click on the Setup. -> Select the Object Manager tab.

Validation rule salesforce

Step 2. Select the Object on which you want to create the Validation Rule. Here, I’m going to create an Account Object.

Salesforce validation rule

Step 3. Now select the Validation Rules from the Account object.

Salesforce validation rule for text field

Step 4. Now select the New button for creating the new validation rule.

Add validation rule for text field in salesforce

Step 5. Now, enter the Rule Name and check the Active checkbox to apply the rule. Then, enter the logic for Error Conditions in Formula Editor.

ISBLANK( Site ) || 
NOT(REGEX( Site, "^[^0-9]*$"))
  • ISBLANCK(): 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.
See also  How to Create Workflow in Salesforce

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

Validation Rule in Salesforce

Step 6. 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

Step 7. 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

This is how to 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 the out-of-range numbers. For example, we have the Rating_0_5__c field on an Account between 0 and 5.

Step 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.

Step 2: Now, enter the Rule Name and check the Active checkbox to apply the rule. Then, enter the logic for Error Conditions in Formula Editor.

OR( Ratings_0_5__c  < 0,  Ratings_0_5__c  > 5)

This formula will display an error when the user does not enter the Number Field in the specific range(0-5).

Apply Validation Rule on Number Field in Salesforce

Step 3: Now enter Error Message and Error Location. Here, I have selected on 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

Step 4: Here, you can see 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.

See also  How to Freeze and Unfreeze User 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.

Step 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.

Step 2: Now, enter the Rule Name and check the Active checkbox to apply the rule. Then, enter the logic for Error Conditions in 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 the close date as today or less than today.

Validation Rule on Date Field in Salesforce

Step 3: 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

Step 4: Today’s date is 20/05/2024. However, when the user selects 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 also how to create and apply validation rules to different Fields in Salesforce.

You may like to read: