Validation rules
I bet you already know what a validation rule is, but let’s discuss them again. A validation rule is a feature that is used to improve data quality. It is checked before a record is saved in the database.
They are defined by the following:
- A name
- An Active flag
- A condition (formula) that should trigger the error (a chain of conditions on fields of the object or related objects that triggers an error if evaluated as true)
- An error message that is displayed to the user
- A place where the error can be displayed
Here is an example of a validation rule:
Example of a simple validation rule
This validation rule prevents a user from inserting a negative value in the Invoice Amount field:
Validation rule triggers if its conditions are met
Some of its configuration fields are easy to understand:
- Error Message: Error displayed to the user if a condition is met.
- Error Location: Where the error should be placed. This can be set to Top of Page or to a specific field (this field must be an editable field, so formulas or roll-up fields are excluded)
As for the condition, we have to use a formula by referencing fields coming from the current object or related objects (we’ve seen a cross-object formula earlier in this chapter)—that is, the error condition that makes the validation trigger and display the error message to the user.
What if you have more than one validation rule? When saving a record, Salesforce checks all validation rules, and even if a rule triggers an error, all other rules are run so that the user can see all error messages.
Validation rules are executed before any other automation action in the following order:
- Validation rules
- Assignment rules
- Autoresponse rules
- Workflow rules (with immediate actions)
- Escalation rules
Note that a workflow rule can update a record, setting its fields to invalid values (against rules defined in the validation rules) just because workflow rules don’t trigger validation rules.
At first sight, validation rules overlap with the lookup filter feature. The lookup filter has a much better user experience because it leads the user to a quicker reference selection, anticipating any error.
Use validation rules if you have reached the limit of lookup filters. You can also use validation rules if the logic involved in the rule’s condition is way more complex than the one you can create with basic lookup filters.
You don’t have to remember any specific field name available; you can use the advanced formula utility (the Insert Field button on the validation rule editing page):
Field selector on a formula editor
You can get the following:
- All current objects’ fields and related objects’ fields (in this case, we are building a VR for the Opportunity object)
- $ObjectType: Used to reference specific fields on custom objects (refer to the following VLOOKUP section)
- $Organization: Current organization fields (such as the name, city, language, locale, everything you can find in the Setup | Company Settings | Company Information | Organization Detail section)
- $Profile, $User, $UserRole: Current user, profile, and role details (this can be used to prevent selected users from setting specific fields in various conditions—for example, a portal user cannot close a Case)
- $System: System fields
Let’s have a look at some advanced functions that you can use within rules (and formulas in general):
- VLOOKUP
- REGEX
- PRIORVALUE
For the entire reference of available functions in formulas, refer to https://help.salesforce.com/articleView?id=customize_functions.htm&type=5.