Validation Rules
What is a validation Rule? Validation rule helps you to improve data quality by preventing users from entering incorrect data. We can write one or more validation rules that consist of an error and corresponding error message.
1. Validation rules are executed, when you are saving the record.
2. A validation rule that contains a formula or an expression that evaluates the data in one or more fields and returns a value, true or false. We can display error message at the top of the page or below the field when rule returns true.
3. After writing validation rules for a field or for a set of fields, following actions will be fired when a user creates a new record or edits an existing record and then click on save button.
- Salesforce executes validation rules you defined and if data is valid then record will save.
- If entered invalid data, it will display the associated error message without saving the record.
- Even if the fields referenced in the validation rules are not visible on the page layouts, the validation rule still apply and will result in an error message if the rule fails.
Creating validation rules:
For standard objects Go to setup -> Build – > Customize -> select standard object you want to create validationrule (For Ex: Account) -> and click on validation rules and then define your validationrule.
For custom objects Go to setup -> Build -> create -> object -> select object you want to create validationrules -> go to validation rules section and then create your validationrule.
Examples of validationrules:
Example 1: In this example i will make field required depends on the other field value.
Here i am writing validationrule on account object. “If Industry equal to Banking then annual revenue should not be null”.
Steps to write this validationrule.
Go to setup -> Build -> customize -> Account -> Validationrule -> click on NEW and enter required fields and click on save.
See below information and image for reference.
Rule Name: Validation_on_Account_revenue
Active : True
Error condition Formula: AND( ISPICKVAL( Industry , ‘Banking’),ISBLANK(AnnualRevenue) )
Error Message: Annual Revenue should not null when Industry is Banking.
Error Location: at field Annual Revenue.
How to test this validation rule?
Go to Account Tab and create one new account record and give Industry = Banking and Annual Revenue = Null and click on save button, you will get error message. See the below screen for reference.