Salesforce Developer Data Validation

salesforce-developer

https://trailhead.salesforce.com/force_com_dev_beginner/point_click_business_logic/validation_rules

// Salesforce - Developer - Data Validation:

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.  (Validation rules also include an 
error message to display to the user when the rule returns a value of “True” 
due to an invalid value.) 

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.

OR (ISBLANK (field__c), field__c<>1)

AND (ISPICKVAL(StageName, "Closed Won"), ISBLANK(Project_Start_Date__c))

NOT(OR(ISNEW(),HasOpportunityLineItem))

// To create 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:

   1. Rule Name: Unique identifier of up to 40 characters with no spaces or 
      special characters such as extended characters.

   2. Active: Checkbox that indicates if the rule is enabled. This is a toggle.

   3. Description: A 255-character or less description that distinguishes the 
      validation rule from others. For internal purposes only.

   4. Error Condition Formula: The expression used to validate the field.

   5. Error Message: The message that displays to the user when a field fails 
      the validation rule.

   6. 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.

// Example 1 (Account Number Is Numeric):

1. Description: Validates that the Account Number is numeric if not blank.

2. Formula: AND ( NOT(ISBLANK(AccountNumber)), NOT(ISNUMBER(AccountNumber)))

3. Error Message: Account Number is not numeric.

4. Error Location: Account Number

Notice how the various parts of the AND condition are separated by commas.

// Example 2 (Date Must Be in the Current Year):

1. Description: Validates that a custom date field contains a date within the current year.

2. Formula: YEAR( My_Date__c ) <> YEAR ( TODAY() )

3. Error Message: Date must be in the current year.

4. Error Location: My Date

The above formula is just a simple formula.

// Example 3 (Number Range Validation):

1. Description: Validates that the range between two custom fields, 
    Salary Min and Salary Max, is no greater than $20,000.

2. Formula: (Salary_Max__c - Salary_Min__c) > 20000

3. Error Message: Salary range must be within $20,000. Adjust the 
    Salary Max or Salary Min values.

4. Error Location: Salary Max

The above formula is a simple formula.  See http://sforce.co/2hVA49c for 
more examples.

// Cloning formula:

We 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.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License