Time Estimate

Topics

Create Validation Rules

Learning Objectives

After completing this unit, you'll 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 that fires when a user tries to save an account with an account number of incorrect length.

Creating a Validation Rule

  1. From Setup, go to Object Manager and click Account.
  2. In the left sidebar, click Validation Rules.
  3. Click New.
  4. Enter the following properties for your validation rule:
    1. Rule Name: Account_Number_8_Characters
    2. Error Condition Formula:
      LEN( AccountNumber) != 8
  5. Error Message: Account number must be 8 characters long.
  6. To check your formula for errors, click Check Syntax.
  7. 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.
Formula:
	AND(
   NOT(ISBLANK(AccountNumber)),
   NOT(ISNUMBER(AccountNumber))
)
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.
Formula:
(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.
Formula:
AND(
   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.
Formula:
OR(
LEN(BillingCountry) = 1,
NOT(
CONTAINS(
"AF:AX:AL:DZ:AS:AD:AO:AI:AQ:AG:AR:AM:" &
"AW:AU:AZ:BS:BH:BD:BB:BY:BE:BZ:BJ:BM:BT:BO:" &
"BA:BW:BV:BR:IO:BN:BG:BF:BI:KH:CM:CA:CV:KY:" &
"CF:TD:CL:CN:CX:CC:CO:KM:CG:CD:CK:CR:CI:HR:" &
"CU:CY:CZ:DK:DJ:DM:DO:EC:EG:SV:GQ:ER:EE:ET:FK:" &
"FO:FJ:FI:FR:GF:PF:TF:GA:GM:GE:DE:GH:GI:GR:GL:" &
"GD:GP:GU:GT:GG:GN:GW:GY:HT:HM:VA:HN:HK:HU:" &
"IS:IN:ID:IR:IQ:IE:IM:IL:IT:JM:JP:JE:JO:KZ:KE:KI:" &
"KP:KR:KW:KG:LA:LV:LB:LS:LR:LY:LI:LT:LU:MO:MK:" &
"MG:MW:MY:MV:ML:MT:MH:MQ:MR:MU:YT:MX:FM:MD:MC:" &
"MC:MN:ME:MS:MA:MZ:MM:MA:NR:NP:NL:AN:NC:NZ:NI:" &
"NE:NG:NU:NF:MP:NO:OM:PK:PW:PS:PA:PG:PY:PE:PH:" &
"PN:PL:PT:PR:QA:RE:RO:RU:RW:SH:KN:LC:PM:VC:WS:" &
"SM:ST:SA:SN:RS:SC:SL:SG:SK:SI:SB:SO:ZA:GS:ES:" &
"LK:SD:SR:SJ:SZ:SE:CH:SY:TW:TJ:TZ:TH:TL:TG:TK:" &
"TO:TT:TN:TR:TM:TC:TV:UG:UA:AE:GB:US:UM:UY:UZ:" &
"VU:VE:VN:VG:VI:WF:EH:YE:ZM:ZW",
BillingCountry)))
Error Message: A valid two-letter country code is required.
Error Location: Billing Country

Resources

Lightning bolt icon used to indicate that the content is for Lightning Experience

Remember, this module is meant for Lightning Experience. When you launch your hands-on org, switch to Lightning Experience to complete this challenge.

retargeting