Create a Validation Rule


Dirty data: It could be incomplete, inconsistent, outdated, duplicated, or full of spelling errors. No matter what, it leads to confusion, time waste, budget waste, and internal and external mistrust. And no one wants that. You’ve probably spent more time than you like cleaning up messy data in your org. But how do you keep data from getting dirty in the first place? The answer doesn’t involve bleach or hand sanitizer, just setting up a few rules, formulas, filters, and processes.

Follow Along with Trail Together

Want to follow along with an instructor as you work through this step? Take a look at this video, part of the Trail Together series on Trailhead Live. You can find a link to the full session in the Resources section.

Setting Limits with Validation Rules

Let’s start with validation rules, which allow you to set up business-specific criteria to prevent users from saving invalid data in one or more fields. A validation rule evaluates a formula upon record save. If a rule’s criteria aren’t met, users see a custom error message and the record doesn’t save. If a rule’s criteria are met, the record saves. Use validation rules to improve data quality by applying conditions, ensuring proper formatting, and enforcing consistency.

As the admin for AW Computing, your first keep-it-clean task is at the request of Allison Wheeler, the VP of Global Sales. She’s aiming to ensure deliveries for opportunities be scheduled within two weeks after an opportunity stage changes to closed won. Use a validation rule to set the time frame.

Before you set up the validation rule, it’s necessary to create a custom field, Delivery Schedule Date.

  1. Click the gear icon Gear icon and select Setup.
  2. Click the Object Manager tab.
  3. Click Opportunity.
  4. Click Fields & Relationships, then click New.
  5. Select Date, then click Next.
  6. For Field Label, enter Delivery Schedule Date.
  7. Click Next, then complete Step 3.
    • In the Read-Only column header, select the checkbox.
    • In the Read-Only column, deselect the Standard User and System Administrator checkboxes.
  8. Click Next, then click Save.

Now create a validation rule on the Opportunity object.

  1. From the Opportunity Object Manager, scroll down and click Validation Rules.
  2. Click New, then create a validation rule with these details.
    Field Value
    Rule Name Delivery_Schedule_14_Days_Or_Less
    Active Select
    Description Sets end date for initial delivery

Now, enter the error condition formula.

  1. From the Functions list, select ISPICKVAL, then click Insert Selected Function.
  2. In the formula, highlight picklist_field.
  3. Click Insert Field, select Stage from the second list, and then click Insert. Note that Stage will convert to StageName in the formula.
  4. In the formula, highlight text_literal, and enter "Closed Won" (including the quotation marks).
  5. Click at the end of the formula to position the cursor.
  6. Click Insert Operator, then select && And.
  7. Click Insert Operator, then select ( Open Parenthesis.
  8. Click Insert Field, select Delivery Schedule Date from the second list, then click Insert.
  9. Click Insert Operator, then select - Subtract
  10. Click Insert Field, select Close Date from the second list, then click Insert.
  11. Click Insert Operator, then select ) Close Parenthesis.
  12. Click Insert Operator, then select > Greater Than and enter 14 at the end of the formula.
  13. Verify that the formula looks like this:
    • ISPICKVAL( StageName , "Closed Won") && ( Delivery_Schedule_Date__c - CloseDate ) > 14
  14. Click Check Syntax.  Delivery Date Formula Screenshot
  15. Next, enter the error message information.
    Field Value
    Error Message Dates of type delivery must be no later than 14 days after close date
    Error Location Field: Delivery Schedule Date
  16. Click Save.

You’ve gotten off to a good start in the AW Computing dirty data prevention plan by creating a validation rule. Now move on to the next step, and have some good, clean fun with formula fields.


Keep learning for
Sign up for an account to continue.
What’s in it for you?
  • 1 in 4 land a new job
  • 50% receive a promotion or raise
  • 80% learn new technologies that boost their resume
  • 66% say it increases productivity
Source: Trailblazer Community Impact Survey 2019