SALESFORCE USEFUL VALIDATION RULES

Improve the quality of your data using 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”. 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.

After you have defined validation rules:

  1. The user chooses to create a new record or edit an existing record.
  2. The user clicks Save.
  3. All validation rules are verified.
  • If all data is valid, the record is saved.
  • If any data is invalid, the associated error message displays without saving the record. 
    4.The user makes the necessary changes and clicks Save again.

There are many operators and functions which we can use in salesforce validation.
The most important and tipical is use to REGEX function in validation.

A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards. You are probably familiar with wildcard notations such as *.txt to find all text files in a file manager.

REGEX: Compares a text field to a regular expression and returns TRUE if there is a match. Otherwise, it returns FALSE. A regular expression is a string used to describe a format of a string according to certain syntax rules.

How to use regex:
REGEX(text_field, regex_text) replace regex_text with the regular expression you want to match.

Validation Rule Example:
1. This example ensures that a custom field called SSN matches a regular expression representing a valid social security number format of the form 999-99-9999.

Regex to validate user input in SSN field: "[0-9]{3}-[0-9]{2}-[0-9]{4}"
[0-9] => 0 through 9.
{3}   => 3 times.
So [0-9]{3} => 0 through 9 3 times.

Function:
NOT(
    OR(
LEN (SSN__c) = 0,
REGEX(SSN__c, "[0-9]{3}-[0-9]{2}-[0-9]{4}")
    )
)

2. Validates that the account Billing Zip/Postal Code is in the correct format if Billing Country is Canada. Canadian postal code must be in A9A 9A9 format.

Regex to validate user input in account Billing Zip/Postal Code field: "((?i)[ABCEGHJKLMNPRSTVXY]\\d[A-Z]?\\s?\\d[A-Z]\\d)?"

?i =>   Case-insensitive matching enabled
[ABCEGHJKLMNPRSTVXY] => A, B... or Y
\ => Nothing, but quotes the following character
\d =>   A digit: [0-9]
\s => A whitespace character
[A-Z] =>  A through Z
X? => X, once or not at all

Function:
AND(
OR(
BillingCountry = "CAN",
BillingCountry = "CA",
BillingCountry = "Canada"
),
NOT(
REGEX(
BillingPostalCode,
"((?i)[ABCEGHJKLMNPRSTVXY]\\d[A-Z]?\\s?\\d[A-Z]\\d)?"
)
)
)


No comments:

Post a Comment