Validation Rules in Salesforce

When you take inputs from Users, the most important thing to be considered is that we get clean and good data. For this purpose, Salesforce provide us the helpful feature called as Validation Rule to validate the fields depends on your business criteria. 

Rules in Salesforce

There are five types of rules in Salesforce: 

  1. Validation Rules
  2. Assignment Rules
  3. Auto-response Rules 
  4. Workflow Rules
  5. Escalation Rules
In today's tutorial, we are going through Validation Rules with an example. So, Let's start today's session.

Introduction To Validation Rules

Validation Rules is used to validate the user input while creating or manipulating the records. It allows you to define custom logic and error messages to ensure data integrity. This Validation Rules fire after clicking on the save button. It evaluates the data in one or more fields and return a value of True or False.

It return error message when the data entered by the user is invalid or we can say that when the validation rule return a value True, then an error message will be displayed on page.

Validation Rules consists of :

Formula: Criteria to validate business rule. If formula evaluates to true, validation error is thrown. 

Error Message: This message will appear when condition formula is true.

Error Location: It defines location to display error message. The error message can be either displayed at the top of the page or below a specific field on the page.

Now, Let's get your hands dirty with it.

Business Case (Account Number Length Validation): Validates that the Account Number is exactly 10 digits.

  1. Go to Setup. 
  2. From Object Manager, Open Account Object. 
  3. On the bottom of the left sliding tab, you'll find Validation Rules, click on it. Then Click on New.

  4. Give a Rule Name, Let's name it Account_Number_Length (you can put any name of your choice).
  5. In Formula, put the below condition. This condition evaluates to true if the Account Number Field length is not equals to 10.
  6. LEN(AccountNumber) <> 10
  7. Don't forget to check the syntax, to see if there's any error in it.
  8. Then enter an error message and Error Location is Account Number field.
  9. Click on Save. 
Superb, You've created a validation rule, quiet simple but good to start with. Now, let's see it in action. We'll try to enter a account number of any length other than 10.
          
When to use Validation Rules
There are many Scenarios/Business Cases where rules are to be defined, so that user gets appropriate data. Let's see some cases where we can use Validation Rules.
  1. Create Formula to ensure that email address format is valid
  2. Validation rule to validate the Phone Number Length is exactly 10.
  3. Validate that the Case status is Re-opened only when the case is opened again.
  4. Validate that the Date Field value is a weekday ( not Saturday and Sunday).
  5. Use Validation to check that user cannot change the Closed Date of an opportunity to a previous date.
Like above given examples, there are many scenarios where you can use validation rules. 
To know more about, go to Examples of Validation Rules.

Also take a look at the operators and functions that are provided and understand their use. The trick to getting good at it is to first understand the requirements well and then see if we can implement this using Validation Rules. If yes, then how can we do that?
         
That's all for the validation rules, still if you have any further query, suggestions and question on to this or if you want me to write a blog on some custom/specific topic then just make sure to comment down and let me know.

Thank You!!

Comments