What is a validation Rule? Validation rules helps you to improve data quality by preventing users form entering incorrect data. We can write one or more validation rules that consists of an error and corresponding error message.
1. Validationrules are executed will 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 validationrules for a field or for set of fields, following actions will fired when user creates a new record or edits an existing records and then click on save button.
- Salesforce executes validationrules 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 validation rule.
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.