Playing With Validation Rules
Using validation rules we can improve the quality of our data. Validation rules verify that the data a user enters in a fields meets the standards you specify before the user can save the record. A validation rule can contain an expression or formula and that evaluates the data in one or more fields based on the requirement and then returns a value either in “True” or “False”.
Please make sure whenever you are going to write a validation rule that validation rule must be written in a way so that it returns a values as “True” due to an invalid value then only it will throw an error message which you defined during the creation of validation rule.
After you have defined validation rules, the user chooses to create a new record or edit an existing record based on the requirement.
Once user clicks on Save button. All validation rules are verified. If all data is valid, the record is saved successfully. If any data is invalid, the associated error message displays as well as it won’t allow to save entered record. The user makes the necessary changes and Save it again.
You can define an error message to display when a record fails validation and where to display it. For example, your error message can be “Stock Exceeded..”You can choose to display it near a field (due to that field, its throwing an error) or at the top of the page. Like all other error messages, validation rule errors display in red color text and are preceded by the word “Error”.
How to create a Validation Rule :
- Open your object on which you want to apply validation (e.g. here we are choosing Line Item object), scroll down until you get Validation Rules section as specified below and click New button.
- . Once you click on New button, below screen will appear, where you need to define your validation rules (so here we are trying to make you understand that if Line item stock (i.e. Selling stock) is greater than Merchandise Stock (i.e. the stock which we have in our Warehouse) then error message should be encounterred and for this we need to write an Error Condition Formula : Line_Item_Stock__c > Merchandise__r.Merchandise_Stock__c ) and click Save button
Note: So whenever your LineItem Stock is greater than Merchandise Stock either creating new record or update an existing record, it will throw an error, even if the fields referenced in the validation rule are not included in an API call or page layout.
Validation rules don’t apply in the scenario where you create new records for an object with Quick Create.