0% found this document useful (0 votes)
423 views

Validation Rule

Validation rules enforce data quality by validating field values before records are inserted or updated. They can be used to require fields, check relationships between fields, and enforce picklist values, data formats, and other business rules. Validation rules run anytime a record is inserted or updated, but not on deletion. There are various functions that can be used to check field values, formats, and relationships in a validation rule formula.

Uploaded by

ravilla naveen
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
423 views

Validation Rule

Validation rules enforce data quality by validating field values before records are inserted or updated. They can be used to require fields, check relationships between fields, and enforce picklist values, data formats, and other business rules. Validation rules run anytime a record is inserted or updated, but not on deletion. There are various functions that can be used to check field values, formats, and relationships in a validation rule formula.

Uploaded by

ravilla naveen
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

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.

Salesforce provides 2 Types of Validation Rules.

1. Client Side Validations:


============================
Client Side Validations are used to validate the information at the Browser
level.

Note: Salesforce Always recommended to have the validations at the client


side. So that we can improve the application performance.

Note: We can implement the Client Side validations by using "Validation


Rules".

2. Server Side Validations:


============================
Server Side Validations are used to validate the data at the Database Server
level.

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.

Salesforce provides a set of Readymade functions to be used to validation the data.

Functions:
==========

1. ISBlank(<FieldName / API Name>)


====================================
This function is used to verify the specified field value is empty or not. If the
field is empty, then it returns TRUE. Else it returns FALSE.

Note: It support both numeric & text fields.


Ex:
IsBlank(Phone)
IsBlank(website)
isBlank(BillingCity)

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)

2. ISNULL(<FieldName / API Name>):


=======================================
This function returns TRUE, if the specified field value is NULL / Empty. Else it
returns FALSE.

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.

Rule Name: Annual Revenue Is Field Required


Error Condition Formula: IsNull (AnnualRevenue)
Error Message: Please enter Annual Revenue value.
Description: The rule is used to make the Annual Revenue Field as Required

3. Text(<Picklist Field Name>):


================================
Text Function is used to get the currently selected value from the Picklist.

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.

To prepare the Compound conditions by grouping the multiple conditions together we


have to use "Logical Functions".

Salesforce provides the below 3 Logical Functions.

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.

AND( Not isBlank(Email),


isBlank(Phone)
)

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.

It returns TRUE, if it is a New Record. Else it returns FALSE.

New Record URL: https://ap16.salesforce.com/00Q/o

Existing Record URL: https://ap16.salesforce.com/00Q2w000003vU0l

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)
)
)

7. IsPickVal (<Picklist Field Name>, <Text Literal>)


====================================================
This function is used to compare the currently selected value in the specified
Picklist field is equals to the text literal or not.

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 need to prepare the format to be used to compare the field value.

We can prepare the expression / format with the help of "Wild Card Characters".

Salesforce supports the below Wild Card Characters.

[ ] --> Represents a Group of Characters

[ s p a] --> It represents a word, which has been prepared by using the


characters "a s p".

[ a - z] --> It represents a Word, which should be prepared by using the


collection of characters. Where each character should be
between "a - z".

[0-9] --> It represents a Number, which contains the digits between 0 - 9.

{<Integer>} --> It represents the specified number of characters / digits.

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}')

===================================================================================
===================================================================================
================================================================

(1.) Can we avoid deletion of record through validation rule?


No, validation rule are fired in insert and update operation.

(2.) Can we bypass the validation rule?


Validation rule can never be bypassed.

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

(4.) Is it possible to write a validation rule on record delete?


No, only in insert and update events you can write validation rule.

(5.) What are use of validation rule in Salesforce?


Validation rule are used to maintain the data format and consistency.

(6.) How validation rules executed? is it page layout / Visualforce dependent?


The validation rules run at the data model level, so they are not affected by the
UI. Any record that is saveds in Salesforce will run through
the validation rules.

You might also like