Validation Rules in Salesforce

Validation rules are often used to verifying the integrity of a given data or a record. Any platform that deals with databases has validation rules to prevent data integrity and data duplicity. Salesforce is not an exception, and it also allows the creation of validation rules for different org. In Salesforce, the validation rule can be a formula or it can be created in form of expression. The validation rules can evaluate one or more than one field, in Boolean format. That means, either it has to be True else False.  Validation rules can also trigger the error messages in Salesforce, based on the Boolean condition that is returned.

What can be expected after the validation rules are set?

The user will create a new record or edit an existing record. Once the data for the record has been entered, he will click on “Save”, and this event will trigger the validation rules. In case the data is found valid, the record will be saved in the database. However, if the data is invalid, then it will trigger an error message, and the data will not be saved. In the second situation, the user makes some adjustments in the data, by editing it. Then he saves the data again. Salesforce allows displaying customized error messages in case the validation rule fails. The error message is displayed in red, and it starts with the text “Error”.

How to define a validation rule?

To create a new validation rule, go to the specified object. For example, if you want to validate the AccountID, then go to the Accounts object. Go to the management settings of the selected object, and then click on “Validation Rules”. Now click on “New”. In the next step, the properties of the validation rules need to be set. This includes the following.

  • Rule Name: This will be the name of the validation rule, and it should be unique. Salesforce allows 40 characters for the rule name. There should not be any special characters and or any spaces.
  • Active: This property notifies whether this validation rule is enabled or not. Sometimes, we might create a rule, but we might not want to enable it instantly. Then we just set it as no. If we want to get it enabled post creation, then this field should be set to “Yes”.
  • Description: This will be the description of the validation rule. This will not be viewable by the users and is used only for internal purposes. Salesforce allows 255 characters for setting the description of the validation rule.
  • Error Condition Formula: This is the formula or the expression that will validate the field.
  • Error Message: This will be an error message, that will be triggered if the validation rule failed.
  • Error Location: This field will determine, which region of the page the error message will be displayed. In case you want the error message to be displayed, just beside the field, then select the option field, and then select the specific field. However, if this field is deleted later, then by default Salesforce will display the error message on top of the page.

Managing the Validation Rules

Once a validation rule is defined, there might be requirements where we have to manage the created rule. We might have to update it, or it might be required to be deleted as well.

  • In either case, go the object with which the validation rule is associated.
  • Go to the management properties, and then to the validation rule page. All the created rules will be displayed on this page.
  • Go to the appropriate rule, and then click on the edit option located right beside the rule.
  • Post that the rule fields can be edited, and then saved. In case the validation rule is no longer required, then it can be deleted from the same page.
  • If the validation rule is created, but it is not active, then click on the edit option, and select the active option to Yes. This will make the validation rule active.

Cloning a validation rule

Salesforce allows cloning an existing validation rule to another entity or object.

  • To clone, go to the management settings of the particular object, and then go to the validation rules page. The rules that need to be cloned should be listed here.
  • Now click on the selected validation rule that will be cloned, and then click on “Clone”.
  • Now define the new rule, that will be based on the existing one. The new rule can be defined in the same way as it has been discussed in the previous section.
  • Specify the field values, and then click on “Save”. The new rule is now successfully cloned to the existing one.

Tips for writing “Error Messages”

The error message should be guiding the users to correct input, rather than confusing them. It should be informative, if not elaborative. Because of the limitations in the number of characters we cannot make the error messages as descriptive and elaborative. Hence, flag the error message like “Invalid Entry” but it should be followed with the error type as well. For example, “Invalid Entry: Enter Date incorrect format”. The user will come to straightaway about the error and will correct his input.