Table of Contents
Validation rules in Salesforce can seem daunting and slightly confusing but once you get the hang of it, it’s a delight to work with.
Validation Rules ensure that the data entered in a record by a User meets the standards you specify before they can save it.
Let’s get the hang of the basic concepts before moving forward:
- A Validation Rule is a formula or expression that evaluates or validates the data in one or more fields.
- It Returns a Value of TRUE or FALSE.
- A value of TRUE signifies that the data entered by the User doesn’t meet the conditions you specified, and the error message is displayed. ( This does seem odd at first; shouldn’t we be displaying the message only when the validation rule returns a FALSE? You’ll get the hang of it soon).
- Ensure that the Account Number is Numeric.
- Ensure that the Account Number is Numeric only if the field is not blank.
- Ensure that all phone numbers contain a specific format.
How to create a Validation Rule
Let’s create a Validation Rule for the Account object that’s going to display an error message if the length of the field, AccountNumber is not equal to 8.
NOTE: In Salesforce, the validation rule checks if the error condition is satisfied. If it is satisfied, it returns a value of TRUE, and the error message is displayed to the User.
1. From Setup, open Object Manager, and click Account.
2. From the sidebar on the left, open Validation Rules.
3. Click New.
4. Enter the following properties:
- Rule name: Account_Number_8_Characters
- Error Condition Formula: LEN( AccountNumber ) != 8
- Error Message: Account Number must be 8 characters long!
5. Click Save.
That’s it! You just created your first validation rule in Salesforce. To see the rule in action, create a record of the Account object, and either leave the AccountNumber field empty or enter more or less than 8 characters. The error message should look like this!
1. Account Number Is Numeric
We need to create a validation rule → If the AccountNumber field is not blank, then check if it is numeric.
A couple of points to keep in mind before proceeding:
- Only when the AccountNumber field is not blank AND the data entered is not numeric, should the validation rule return a TRUE(1) and display the error message.
- The function ISBLANK() returns TRUE(1) if the field is blank.
- The function ISNUMBER() returns TRUE(1) if the data entered is numeric.
- The AND function returns a TRUE(1) only when all inputs are TRUE(1).
- The NOT function returns the inverse of an expression.
- For more clarity, disable the previous Validation Rule (Account_Number_8_Characters) that we created.
The truth table for the rule should look like this:
From the table, we derive the expression (using Sum of Products) for the rule as:
AND ( NOT(ISBLANK(AccountNumber)), NOT(ISNUMBER(AccountNumber)) )
2. Entered Date Must Be in the Current Year
We require a validation rule that ensures that the year of the custom field New Date is equal to the year of today’s date. The expression returns TRUE(1) and displays the error message when the year in the entered date is not equal to the current year.
- The TODAY() function returns the current date.
- The YEAR() function returns the year component of a date.
- The <> (Not Equal) operator determines if a value is not equal to another value. The function returns TRUE(1) if the values are not equal.
Let’s get started! Create a custom field, New Date, with field type, Date, and then apply the validation rule on the field.
To see the validation rule in action, create a new record, and for the field, New Date enter a year different from the current year, and the error message should appear.
3. Number Range Validation
Let’s try something different here! We need to ensure that the difference between two custom fields, Salary Max and Salary Min, is not greater than $5000. The validation rule determines whether the difference between the fields is greater than 5000. If so, the rule returns a value as “TRUE” and thereafter sends an error message.
To get started, create two custom fields → Salary Max and Salary Min. And create a Validation Rule with the following specifications.
Let’s try creating a new record satisfying the error condition. You’ll notice something different here, the error message is right next to the field “Salary Max”. Unless specifically mentioned, the error message always appears at the top of the page.
4. Website Extension Validation
Let’s validate the field, Website, to ensure that its last four characters are from a set of six valid extensions.
- The AND function returns a value as TRUE(1) only when all inputs are TRUE and a value as FALSE if one or more inputs are FALSE.
- The <> (Not Equal) operator evaluates if a value is not equal to another.
- The function RIGHT(text, num_chars) returns the specified number of characters from the end of a text string.
To create the validation rule, follow the specifications mentioned below:
The error message should be similar to this!
5. Valid Billing Country Check
Let’s create a validation rule that determines whether the billing country entered by the user is valid or not.
- The OR function returns a value of TRUE(1) if one or more expressions within the formula are TRUE, and a FALSE only if all expressions are FALSE.
- The function, CONTAINS(text, compare_text) checks if the text contains the specified text, and returns a TRUE if it does.
- The LEN() function returns the length of the specified text.
Here, the validation rule determines whether the length of the field, Billing Country is either one character long OR does not contain a valid billing country code. If either of these conditions appears as TRUE, the validation rule returns a TRUE and sends an error message.
If the user enters a valid billing country, then both expressions in the formula are FALSE and the rule returns a value of FALSE and does not send an error message.
Let’s create a validation rule with the information given below:
Way to go! Let’s see this validation rule in action! Enter an invalid code for the field Billing Country and voila! You should see an error message similar to this.