📢 Attention Salesforce Certified Trailblazers! Maintain your credentials and link your Trailhead and Webassessor accounts by April 19th. Learn more.
close

Create Validation Rules

Validation rules verify that the data a user enters in a record meets the standards you specify before the user can save the record. 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.” They can also include an error message to display to the user when the rule returns a value of “True” due to an invalid value.

You can use validation rules to improve data integrity, make a field conditionally required, prevent the entry of invalid data values directly by users and other systems, and enforce role-specific, profile-specific, or user-specific constraints.

Ling Wu wants to be sure AW Computing recruiters list a hiring manager every time they set up a position in the recruiting app. It’s also come to Ling’s attention that some candidates’ zip code and state don’t match. She’d like a fix for both these issues, which you can accomplish by creating validation rules.

Set Up the First Rule

First create a validation rule requiring a hiring manager be included with each position listing in the recruiting app.

  1. From Setup, click Object Manager and select Position.
  2. Click Validation Rules, then click New.
  3. For the Rule Name, enter Every_Position_Must_Have_a_Hiring_Mgr
  4. Select Active.
  5. Enter this Description: Every position record must have a hiring manager.
  6. Enter the Error Condition Formula:ISBLANK (Hiring_Manager__c) && $Profile.Name <> "System Administrator"
  7. Ensure your screen looks like this:
    Error Condition Formula for Every_Position_Must_Have_a_Hiring_Mgr validation rule.
  8. Click Check Syntax to verify there are no errors.
  9. Enter this Error Message: Every Position must have a Hiring Manager.
  10. Choose Field: Hiring Manager from the Error Location picklist.
  11. Click Save.

Create Custom Objects

Ling’s mismatched state and zip code issue requires a little work before setting up the validation rule. First, you create custom objects for zip code and state, and add fields to each of them.

Let’s start with the zip code custom object.

  1. From Setup, click Object Manager, click Create, then select Custom Object and enter the details.
    Field
    Value
    Label
    Zip Code
    Plural Label
    Zip Codes
    Record Name
    Zip Code
    Data Type
    Text
    Allow reports
    Select
  2. Click Save.

Now add a field to the Zip Code object.

  1. While still viewing the Zip Code in Object Manager, click Fields & Relationships, then click New.
  2. Select Text as the Data Type, click Next, and enter the details.
    • Field Label: State
    • Length: 2
  3. Click Next.
  4. Set the field visible for the Recruiter: Nontechnical, Recruiter Technical, and System Administrator profiles, and click Next.
  5. Click Save & New.

Add a second field to the Zip Code object.

  1. Select the Text as the Data Type, click Next, and enter the details:
  2. Enter the details for the new custom field.
    • Field Label: City
    • Length: 80
  3. Click Next.
  4. Set the field visible for the Recruiter: Nontechnical, Recruiter Technical, and System Administrator profiles, and click Next.
  5. Click Save.

Next, modify the Zip Code page layout so that the fields are displayed in a logical order.

  1. While still viewing the Zip Code in Object Manager, click Page Layouts.
  2. Click Down arrow icon next to Zip Code Layout and select Edit.
  3. In the Information section, drag the fields to rearrange them in the order of City, State, then Zip Code.
  4. Click Save.

Create a Zip Code Tab

Next, create a Zip Code tab in the Recruiting app.  

  1. From Setup, enter Tabs in the Quick Find box, then select Tabs.
  2. In the Custom Object Tabs section, click New.
  3. From the Object picklist, select Zip Code.
  4. Click Magnifying glass and select Map.
  5. Click Next.
  6. Leave the profile as is and click Next.
  7. In the Add to Custom Apps section:
    • Deselect Include Tab and select Recruiting.
    • Ensure Append tab to users’ existing personal customizations is selected
  8. Click Save.

Now create new zip code records.

  1. Click App Launcher iconto open the App Manager, then click Recruiting.
  2. Click the Zip Codes tab.
  3. Click New.
  4. Enter the City, State, and Zip Code for any location.
  5. Click Save.
  6. Repeat to create three to five zip code records. (Note: To look up zip codes for any city, click this link.)

Set Up the Second Rule

Now create a validation rule that checks the zip code entered against a table to validate that the zip code and state match.

  1. Click Object Manager and select Candidate.
  2. Select Validation Rules related list, click New, and fill in the details.
  3. For Rule Name enter Zip Code Consistent with State.
  4. For Description, enter: Validates candidate Zip/Postal Code by looking up the first five characters of the value in a custom object called Zip_Code__c. Error if the zip code is not found or the candidate State does not match the corresponding State in the object.
  5. Enter this Error Condition Formula: 
    VLOOKUP($ObjectType.Zip_Code__c.Fields.State__c, $ObjectType.Zip_Code__c.Fields.Name, LEFT( Zip_Postal_Code__c ,5) ) <> State_Province__c
  6. Ensure your screen looks like this:
    Error Condition Formula for Zip Code Consistent with State validation rule.

  7. Click Check Syntax to verify there are no errors.
  8. For the Error Message, enter Candidate Zip Code does not exist in specified State.
  9. For Error Location, select Field and Zip/Postal Code.
  10. Click Save.

Now that you’ve taken steps to ensure HR has complete data for the positions they create, move on to the next step, where you use formulas and roll-up summaries to help them utilize the data they collect.

retargeting