How to Set Up Validation Rules in Salesforce's Lightning Experience
What are Validation Rules?
Validation rules are important for any system, Salesforce included. They can prevent users from entering invalid data, which can cause errors and make your reports unreliable. Validation rules also prevent users from saving records if the rule returns an error, which helps ensure data integrity across objects in your Salesforce org.
Let's look at an example of how you might use a validation rule. Say you have a custom object that's related to your accounts, like "account history." When adding or updating information on this object, you always want to be sure that the user selects an account record as well (such as "account history"); otherwise things are going to get confusing fast! A validation rule will require users to include this information when adding a new record of type "account history," and it'll help ensure that all records have correct data moving forward.
Now that we've covered what a validation rule is and why it's useful, let's talk about how we create one in Salesforce!
How do Validation Rules Work?
Unlike workflow rules, which can only evaluate a record when it’s saved, validation rules are evaluated every time the record is saved, including both before and after triggers.
If a validation rule evaluates to true, Salesforce displays an error message defined in the rule. The record will not be committed to the database until the error is fixed and all validation rules have been evaluated as false.
But if your trigger or workflow field update formula is set to fire before a save, you may get an error that your field has been updated by another process—even though it hasn't been yet! To avoid this issue, make sure formulas are set to run after the record is saved.
When to Use Validation Rules in Salesforce
You can use validation rules in Salesforce when:
- You want to make sure users are entering correct data. For example, if you want to ensure a user enters only numbers between 1 and 10,000 in a certain field.
- You want to prevent duplicate records. For example, if you don't want two contacts with the same name or two leads with the same email address.
- You want to prevent users from saving records unless they fulfill certain criteria. For example, if you don't want people to save an opportunity unless they provide information about what the lead wants.
- You want to make sure data is entered in a certain field before users can save a record. For example, if your company has sales targets for each region that can't be saved without a value in the target amount field for that region.
- You want to make sure data is entered in a specific format before users save records. For example, if you have phone numbers that need to be more than 10 digits long but less than 15 digits long (since some phone numbers may have an area code).
Examples of Validation Rules in Salesforce
As you've seen so far, validation rules can be used to ensure that a number is greater than a certain value, or that a date is after a certain date. They can also be used to ensure that a string (a series of characters) is the right length.
Ensure anyone who fills the contact form, must fill out their email address
Here's another scenario: you're putting together a contact form for your website, and you want to make sure that anyone who fills it out has entered their email address.
You could do this with the following validation rule:
AND(ISBLANK(Email), NOT(ISBLANK(Website)))
The logic is simple: if the Email field is blank and the Website field is not blank, allow the user to proceed. If either of those two things aren't true—if both fields are blank or if the Email field is not blank—the rule will throw an error telling the user that they need to enter their email address in order to continue.
Using LEN function
Here's another example of how you can use the LEN function in combination with the AND function to ensure that Account Name must always have more than 5 characters.
The AND function is one of the most common validation rules in Salesforce. To use it, you must combine it with a function like LEN (length) to indicate how many characters you want your string to be, and then include an error message that will pop up if someone doesn't meet the requirements.
AND(LEN(Name) > 5, false)
Salesforce validation rules let you enforce the integrity of your data when it's entered.
Salesforce validation rules let you enforce the integrity of your data when it’s entered.
Validation rules check the data that is entered into a record, and ensure that it meets the standards specified by your business before you save the record to Salesforce. This means that you can prevent users from entering incorrect data into your Salesforce org in the first place.