Search Here

Types of Validation Rules


 

Validation rules are an essential aspect of maintaining data accuracy and consistency in Salesforce. There are different types of validation rules that can be used to ensure that the data entered into fields is valid and meets specific requirements. Here are some of the main types of validation rules:


Data Type Check

A data type check ensures that the data entered into a field is of the correct type. The syntax for a data type check is as follows:


ISBLANK(fieldname) || NOT(ISPICKVAL(fieldname, "datatype"))

Where “fieldname” is the name of the field being validated, and “datatype” is the expected data type. 


For example, if we want to ensure that the “Age” field is a numeric value, we can use the following code:


ISBLANK(Age) || NOT(ISPICKVAL(Age, "Number"))

Code Check

A code check ensures that the data entered into a field meets certain criteria based on a specific code. The syntax for a code check is as follows:


fieldvalue < codevalue

Where “fieldvalue” is the value entered into the field being validated, and “codevalue” is the value used for comparison. For example, if we want to ensure that the “Price” field is less than $100, we can use the following code:


Price < 100

Range Check

A range check ensures that the data entered into a field falls within a specific range. The syntax for a range check is as follows:


fieldvalue >= lowervalue && fieldvalue <= uppervalue

Where “fieldvalue” is the value entered into the field being validated, “lowervalue” is the minimum value in the range, and “uppervalue” is the maximum value in the range. 


For example, if we want to ensure that the “Quantity” field is between 1 and 10, we can use the following code:


Quantity >= 1 && Quantity <= 10

Format Check

A format check ensures that the data entered into a field matches a specific format, such as an email address or phone number. The syntax for a format check depends on the specific format being validated. 


For example, if we want to ensure that the “Email” field is in a valid email format, we can use the following regular expression:


REGEX(fieldname, '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')

Uniqueness Check

A uniqueness check ensures that the data entered into a field is unique within a specific context, such as within a specific record type or object. The syntax for a uniqueness check depends on the specific context being validated. 


For example, if we want to ensure that the “Username” field is unique within the “User” object, we can use the following code:


ISNEW() || Username__c != PRIORVALUE(Username__c)

Presence Check

A presence check ensures that a field is not left blank. The syntax for a presence check is as follows:


ISBLANK(fieldname)

Where “fieldname” is the name of the field being validated. For example, if we want to ensure that the “Name” field is not left blank, we can use the following code:


ISBLANK(Name)

Length Check

A length check ensures that the data entered into a field meets certain length requirements. The syntax for a length check depends on the specific length requirements being validated. 


For example, if we want to ensure that the “Description” field is between 10 and 100 characters, we can use the following code:


LEN(Description) >= 10 && LEN(Description) <= 100

Look Up

A look-up validation rule ensures that a value entered in one field matches the value in 


another field in a related object. The syntax for a look-up validation rule is as follows:


value1__c = related_object__r.value2__c

Where “value1__c” is the value entered in the first field and “related_object__r.value2__c” is the value in the corresponding field in the related object. 


For example, if we want to ensure that the “Contact” field in the “Opportunity” object matches the “Primary Contact” field in the related “Account” object, we can use the following code:


Contact__c = Account__r.Primary_Contact__c

Consistency Check

A consistency check ensures that the data entered into multiple fields is consistent with each other. The syntax for a consistency check depends on the specific fields being validated. 


For example, if we want to ensure that the “Start Date” field is earlier than the “End Date” field, we can use the following code:


Start_Date__c < End_Date__c


Useful Formula for Data Validation

Yes, there are formulas that can be used in validation rules in Salesforce. These formulas are used to evaluate certain conditions and determine whether the data entered into a field is valid or not. Here are some examples of formulas that can be used in validation rules:


IF Formula

The IF formula is used to test a condition, return a value if it is true, and return a different value if it is untrue. To make sure the “Discount” field is less than or equal to 50%, for instance, apply the formula below:


IF(Discount__c <= 0.5, true, false)

AND Formula

When evaluating multiple conditions, the AND formula returns true if all conditions are satisfied and false if any condition is not satisfied. To make sure the “Start Date” and “End Date” fields are not empty, for instance, use the formula below:


AND(

  NOT(ISBLANK(Start_Date__c)),

  NOT(ISBLANK(End_Date__c))

)

OR Formula

The OR formula is used to evaluate multiple conditions and return true if any condition is met, and false if none of the conditions are met. For example, the following formula can be used to ensure that either the “Phone” or “Email” field is not blank:


OR(

  NOT(ISBLANK(Phone__c)),

  NOT(ISBLANK(Email__c))

)

ISNEW Function

To ascertain whether a record is being created for the first time, utilize the ISNEW function. This helps to guarantee that specific fields are necessary when a new record is created. For example, the following formula can be used to ensure that the “Name” field is not blank when a new record is created:


ISNEW() && ISBLANK(Name)

PRIORVALUE Function

The PRIORVALUE function is used to retrieve the previous value of a field before it was updated. This is useful for ensuring that certain fields are not changed after they have been set. For example, the following formula can be used to ensure that the “Opportunity Stage” field is not changed after it has been set to “Closed Won”:


ISCHANGED(StageName) && PRIORVALUE(StageName) = 'Closed Won'

Validation rules and formula fields are both used to evaluate data in Salesforce, but they serve different purposes. The table below enumerates the same.

Validation RulesFormula Fields
These are used to validate data before it is saved to ensure data quality.These calculate a value based on other fields and save it to the record.
They apply to one or more fields and prevent records from being saved if invalid.They use a formula that returns a text, number, date, or Boolean value. 
It uses a formula that returns either TRUE or FALSE.It does not show the users any error message, as it fires after the record is saved.  
They show the users an error message if the validation fails.Values can be seen and filtered like regular fields. These are useful for reports and dashboards.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.