Formulas | Validation rules
Validation rules verify that data entered by users in records meet the standards you specify before they can save it. A validation rule contains a formula that evaluates the data in one or more fields and returns a Boolean value of True or False. The validation rules can use the operators and functions available in formula fields. Some additional functions such as REGEX
are available.
Validation rules also include error messages to display to users when they enter invalid values based on specified criteria. Using these rules effectively contributes to quality data. For example, you can ensure that all SSN or NI numbers contain a specified format.
Best practices
Validation rules can be very useful for ensuring quality data. However, excessive use of validation rules can cause a range of issues, such as preventing records from saving and preventing system automation such as Payflow services from running. This is particularly common during the implementation phase, when incomplete data is used for testing. Often the root cause of the resulting issues is not immediately clear. For example, a validation rule in the Team Member object can prevent an Employment Record from saving, or prevents a team member from appearing in a Payflow export.
For these reasons, use validation rules sparingly only for the most important data checks.
You can disable validation rules temporarily by unchecking the Active checkbox in the validation rule. Doing this before importing records ensures that any validation rules set on the object that you are importing to do not disrupt the data load. Temporarily disabling validation rules can cause issues later on when the validation rule is active again, and you try to edit a record failing the validation rule and cannot save it until you fix the cause of the validation rule error.
Create a validation rule
To create a validation rule:
- Go to Setup > Object Manager, and select the object where you need the rule.
- From the left sidebar select Validation Rules.
- Select New.
- Enter the following properties for your validation rule:
Field Description Rule Name Unique identifier of up to 40 characters with no spaces or special characters such as extended characters. Active Checkbox, checked by default. The validation rule operates when the checkbox is checked.
Description A description to explain what the validation rule does, displayed when viewing the validation rule details. Error Condition Formula The formula used to validate the data.
Error Message The message that displays to the user when a field fails the validation rule.
Error Location Determines where on the page to display the error. To display the error next to a field, select Field and then choose the field. If the error location is a field, the validation rule is also listed on the detail page of that field. - To check your formula for errors, select Check Syntax.
- Select Save.
The REGEX function
The REGEX
function compares a text field to a regular expression—a sequence of symbols or characters in a string—and returns True if there is a match. Otherwise, the function returns False.
The REGEX
function is available for validation rules, but not for formula fields. It is typically used for validating tax numbers such as NI Number in the UK or Social Security Number in the US.
For example, to use the REGEX
function to validate NI numbers:
-
In the validation rule editor, select the
REGEX
function. The syntax is displayed in the formula editor field: -
Replace
Text
with the field to validate: -
Replace
RegEx_Text
with the regular expression as shown in the following example: -
Because the intention is to display an error when the entered value does not pass validation, add the
NOT
function to invert the formula:CopyValidation rule formula with REGEX and NOT
NOT(REGEX(NI_or_SS_Number_c, "[A-Z]{2}[0-9]{6}[A-Z]{1}"))