Skip to main content
Estimación de tiempo

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. For Error Location, select Field, then select Hiring Manager from the dropdown.
  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, create a custom object for zip code, and add fields.

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.
    • 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 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
  1. Click Next.
  2. Ensure the field is visible for the Recruiter: Nontechnical, Recruiter Technical, and System Administrator profiles, and click Next.
  3. 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
  1. Click Next.
  2. Ensure the field is visible for the Recruiter: Nontechnical, Recruiter Technical, and System Administrator profiles, and click Next.
  3. 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.
    • Ensure Append tab to users’ existing personal customizations is selected
  1. Click Save.

Now create new ZIP code records.

  1. Click App Launcher icon to open the App Launcher, then click Recruiting.
  2. Click the pencil icon in the top right corner of the screen.
  3. Click Add More Items.
  4. From the menu on the left, select All.
  5. Click the plus sign next to Zip Codes.
  6. Click Add 1 Nav Item.
  7. Click Save.
  8. Click the Zip Codes tab.
  9. Click New.
  10. Enter a city, state and zip code.
  11. Click Save & New and enter a new city, state and zip code.
  12. Click Save.

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. From Setup, Click Object Manager and select Candidate.
  2. Click Validation Rules, 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, then select Zip/Postal Code from the dropdown.
  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.

Comparta sus comentarios sobre Trailhead en la Ayuda de Salesforce.

Nos encantaría conocer su experiencia con Trailhead. Ahora puede acceder al nuevo formulario de comentarios cuando quiera desde el sitio de la Ayuda de Salesforce.

Más información Continuar para compartir comentarios