Learning Objectives

After completing this unit, you will be able to:
  • Describe two use cases for validation rules.
  • List the elements of a validation rule.
  • Create a validation rule.

Introduction to 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 can contain a formula or expression that evaluates the data in one or more fields and returns a value of “True” or “False.” Validation rules can 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 phone number fields contain a specified format or that discounts applied to certain products never exceed a defined percentage.

Defining Validation Rules

You can create validation rules for objects, fields, campaign members, or case milestones. In these steps, we’ll create a validation rule for a field.

Creating a Validation Rule

  1. Navigate to Customize | Your Object | Fields | Your Selected Field (such as Account Name), and under Validation Rules, click New.
  2. 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 that indicates if the rule is enabled. This is a toggle.
    Description A 255-character or less description that distinguishes the validation rule from others. For internal purposes only.
    Error Condition Formula The expression used to validate the field.
    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, choose Field and select the field. If the error location is a field, the validation rule is also listed on the detail page of that field.
  3. To check your formula for errors, click Check Syntax.
  4. Click Save to finish.
Here’s how a validation rule’s error message can appear when a user types an incorrect phone number format into a field.
A filled out validation rule, including a related error message.

Examples of Validation Rules

Here are some validation rule examples that you can try out yourself:

Account Number Is Numeric

Field Value
Description: Validates that the Account Number is numeric if not blank.
Error Message: Account Number is not numeric.
Error Location: Account Number

Date Must Be in the Current Year

Field Value
Description: Validates that a custom date field contains a date within the current year.
Formula: YEAR( My_Date__c ) <> YEAR ( TODAY() )
Error Message: Date must be in the current year.
Error Location: My Date

Number Range Validation

Field Value
Description: Validates that the range between two custom fields, Salary Min and Salary Max, is no greater than $20,000.
(Salary_Max__c - Salary_Min__c) > 20000
Error Message: Salary range must be within $20,000. Adjust the Salary Max or Salary Min values.
Error Location: Salary Max

Website Extension

Field Value
Description: Validates a custom field called Web Site to ensure its last four characters are in an explicit set of valid website extensions.
   RIGHT( Web_Site__c, 4) <> ".COM",
   RIGHT( Web_Site__c, 4) <> ".com",
   RIGHT( Web_Site__c, 4) <> ".ORG",
   RIGHT( Web_Site__c, 4) <> ".org",
   RIGHT( Web_Site__c, 4) <> ".NET",
   RIGHT( Web_Site__c, 4) <> ".net"
Error Message: Web Site must have an extension of .com, .org, or .net.
Error Location: Web Site

Valid Billing Country

Field Value
Description: Validates that the account Billing Country is a valid ISO 3166 two-letter code.
LEN(BillingCountry) = 1,
Error Message: A valid two-letter country code is required.
Error Location: Billing Country

Tell Me More...

You can save time by cloning an existing validation rule you created.

  1. Navigate to the relevant object, field, campaign member, or case milestone.
  2. In the Validation Rules related list, click the name of the validation rule.
  3. Click Clone.
  4. Define the new rule based on the original rule.
  5. Click Save to finish or Save & New to create additional validation rules.
Share Time Estimate

Having trouble with your challenge verification?

Here are some tips:

  1. Check for typos (hey, it happens)
  2. Try using a new Developer Edition (existing customizations can interfere with the validation)