Validation Rule
Validation Rule
================
Validation Rules are used to enforce the users to enter the correct information
before inserting / updating the records inside the object.
By using Validation Rules, we can maintain the Accuracy and Quality of data inside
the objects.
Note: Validation rule will get fired upon saving the record into the object. Upon
updating the existing record also validation rule will get fired.
Note: We can implement the Server Side validation rules by using "Apex
Triggers".
Governor Limits:
==================
In Free Developer Edition:
We can have max. of 100 Active Validation Rules per an object.
In Unlimited Edition:
We can have max. of 500 Active Validation Rules per an object.
Functions:
==========
Use Case:
===========
Create a Validation Rule to make the Phone Field as required upon creating an
account record inside the object.
Validation Condition:
ISBLANK (Phone)
Note: This function should be applicable only on Numerical values. (Ex: Currency
Fields, Number Fields and Percent fields)
UseCase:
============
Create a Validation Rule to make the Account Record Annual Revenue is Mandatory.
Validation Condition:
IsNull(AnnualRevenue)
Use case:
============
Create a validation rule, to make the "AnnualRevenue" field as mandatory upon
creating the lead record in lead object.
Ex:
Text(Rating)
Text(Industry)
Text(Active__C)
UseCase:
===========
Create a Validation Rule, to make the "Industry Field" Required upon Creating /
updating the Account Record.
Formula Condition:
isBlank( Text(Industry) )
4. Logical Functions:
=========================
Upon validating the data by using Validation Rules, in few cases singe Condition
may not be sufficient. We need to group multiple conditions together to validate
the data.
1. AND()
2. OR()
3. NOT()
AND() Function:
==================
It returns TRUE, if all the conditions are satisfied. And it returns FALSE, if any
of the condition fails.
Syntax:
And(<Condition1>, <Condition2> ,<Condition3> ,<Condition4>... )
OR() Function:
================
It returns TRUE, if any of the condition satisfied. And it returns FALSE, if all
the conditions are failed.
Syntax:
OR(<Condition1>, <Condition2> ,<Condition3> ,<Condition4>... )
NOT Function:
================
This is used to change the result of the operation from True to False and Vice-
versa.
Use Case:
---------
Create a validation rule to make ownership as mandatory, upon rating has been
selected.
and (
not IsBlank (Text(Rating)),
IsBlank (Text(Ownership))
)
UseCase:
--------
Create a Validation Rule on the Contact object, to make sure the "Phone Field
should be Required", if the Email has been entered.
5. IsChanged() Function:
=========================
This function is used to check whether the specified field value has been changed
or not.
If the field value has been updated, then it returns TRUE. Else it returns FALSE.
Syntax:
IsChanged(<FieldName>)
Ex:
IsChanged(Phone)
IsChanged(Email)
UseCase:
--------
Create a Validation Rule, to prevent Changing the Position Name.
Validation Condition:
isChanged(Name)
6. IsNew ():
==============
This function is used to verify the user is currently working on a New Record, or
he is updating an existing record.
IsNew() function will identify the record based on the Record ID inside the URL.
UseCase:
--------
Create a Validation Rule on the Lead object, to make sure either "Phone / Email"
should be required for "New Lead Records".
AND(
IsNew(),
And (
isBlank(Phone),
isBlank(Email)
)
)
If both the values are same, it returns TRUE. Else it returns FALSE.
Ex:
IsPickVal(Rating, 'Hot')
IsPickVal(Industry, 'Finance')
IsPickVal(Designation__C, 'Software Engineer')
Use case:
--------
Create a Validation Rule, to make sure the Rating should be "Hot" for Banking
Industry Customers for account object.
And(
isPickVal(Industry, 'Banking')
Not isPickVal(Rating, 'Hot')
)
UseCase:
--------
Create a Validation Rule, to make sure the Active Status should be "Yes" for New
Account Records.
AND(
IsNew(),
Not IsPickVal(Active__C, 'Yes')
)
8. Prior Value(<FieldName>):
=============================
Prior Value means previos value of this field.
This function is used to get the previous value / old value of the specified field.
Ex:
PriorValue(Rating)
PriorValue(Phone)
PriorValue(Location__C)
UseCase
--------
Create a validation rule on Account object, where can not change the rating value
from Cold to Hot directly.
And(
isPickVal (Rating, 'Hot')
isPickVal (Priorvalue (Rating), 'Cold')
)
9. Regex() Function
=========================
This function is used to compare a field value is exist in the specified format or
not.
If the field value is exist in the specified format, then it returns TRUE. Else it
returns FALSE.
Syntax:
Regex(<FieldName>,<RegularExpression>)
We can prepare the expression / format with the help of "Wild Card Characters".
Example:
PAN Number format.
ALLIP-9090-E
[A-Z]{5}[0-9]{4}[A-Z]{1}
[A-Z,a-z]{5}-[0-9]{4}-[A-Z,a-z]{1}
ALLIP9090E
ALLIP9090e
Credit Card Number:
[0-9]{4}-[0-9]{4}-[0-9][4}-[0-9]{4}
UseCase:
--------
Create a Validation Rule, to Validate the PAN Number format.
Condition:
NOT Regex( PAN_Number__c , '[a-z,A-Z]{5}[0-9]{4}[a-z,A-Z]{1}')
UseCase:
--------
Create a Validation Rule, to Validate the Mobile number should be in 10 Digit
Numeric Only.
Condition:
NOT REGEX( Mobb__c , '[0-9]{10}')
===================================================================================
===================================================================================
================================================================
(3.) Is it possible to write a validation rule which will fire only on insert of
record not on update of record ?
Use isnew() function which checks if the formula is running during the creation of
a new record and returns TRUE if it is.
If an existing record is being updated, this function returns FALSE.