If you’re a Salesforce administrator, one of your main concerns is data integrity. But for a typical user, a high standard of data entry is probably low on the priority list.
Salesforce has many tools to help you enforce data standards, including validation rules, which verify that the data entered in a record matches standards you specify. If the record lacks necessary data, the user will see an error message and will not be allowed to save the record until the data meets the standards.
Let’s walk through building a validation rule, using an actual example from the Newfangled org.
When a sales user marks an opportunity as “Closed Unqualified,” we have a custom field called “Unqualified – Reason” to collect the reason that opportunity was unqualified. We want that custom field to be required, but only if the opportunity is set to “Closed Unqualified.” We can accomplish this by adding a validation rule.
First, navigate to the menu for the object you’re interested in. For this, I’m interested in Opportunities, so I navigate to Setup > Build > Customize > Opportunities > Validation Rules. I click “New” to start building my rule.
Enter a descriptive rule name and enter a description of what this rule is enforcing. Make sure the Active box is checked (assuming you’ll be using this rule right away).
The work of the validation rule is done by a formula expression that evaluates data from one or more fields and returns a value of True or False. If the formula evaluates to True, the user will see an error message.
You need to write a formula that describes the exact data entry condition you are trying to avoid. In this case, we need to describe an opportunity record with the “Stage” field set to “Closed Unqualified” and with no value in the “Unqualified – Reason” field.
Salesforce formulas may seem more than a little confusing. And they can be. But fear not! There are many, many online resources with sample formulas that are available for anyone to use. Salesforce has an entire index of sample validation rules, and the Salesforce Success Community is full of Salesforce users helping other users. Chances are good that you’ll find almost exactly the formula you need from an answer to another user’s query, or at least something close to use as a starting point. There’s absolutely no shame in copying and pasting a formula that someone else has written.
There are also a ton of Salesforce resources to help you become a pro at writing Salesforce formulas. Start with this overview of Salesforce formula operators and functions.
Below you can see how I’ve taken our field criteria (Stage = Close Unqualified, Unqualified Reason = blank) and translated that into a formula. If this formula evaluates to true (i.e., both of those field criteria are true), the record won’t save, and the user will see an error message.
The last step is to define your error message. You can enter whatever you want here, and then choose where the error message displays. You can either have it display at the top of the record or inline with a specific field.
Now that I’ve built my rule and made sure it’s active, I need to see if it works.
I’m going to set my opportunity to “Closed Unqualified” and purposefully leave “Unqualified – Reason” blank. Can I save my record?