PREFACE: This post is part of the Zero to Hero series.

Validation rules are kind of like the bumpers that go up along the gutter at the bowling alley. They guide the user down the right path by ensuring that appropriate fields are filled in at the right time and with the correct information. They ensure that the data entered by a user meets a minimum standard before the record can be saved.

There are a lot of use cases for validation rules. One popular use case is to create conditionally required fields. Let’s say that the business wants a picklist field to be required when two other non-required fields are populated. Because this is a conditional requirement, making the picklist required from the start defeats the purpose. However, a validation rule can make the picklist required when the conditions are met.

Other use cases include missing data, conflicting data and data formatting.

In today’s post, we’ll walk through the creation of a few validation rules in our dev orgs as it relates to our vehicle tracking tool we’ve been building, and then I’ll provide some other common validation rules that can be built in your company’s orgs.

Building a Validation Rule

If you’ve gone through the previous Zero to Hero posts, you’ve built out the functionality to track your vehicles and vehicle repairs with Salesforce. The validation rule we’ll build is based on the repair record. Here’s a look at a recent repair record I created for one of my vehicles.

Repair

Notice that the Repairs Suggested checkbox is checked, but the Suggested Repairs field is blank. Because the Suggested Repairs field is not required, and we only want it to be required when the Repairs Suggested checkbox is checked, a validation rule is the perfect solution. Here’s how to build it.

1. Click Setup | Create | Objects | Repairs | Validation Rules | New
2. In the Rule Name field, enter Require Repair Suggestions Text Box
3. Ensure the rule is marked as Active
4. Enter the following description: When the Suggest Repairs checkbox is checked, require a description in the Suggested Repairs text box.

Remember, that even though the description is not required, the best practice is always to fill it out so that at a glance, you know what this validation rule is supposed to do. Here is how the rule should look.

Validation Rule DetailsNow we’ll create the actual validation rule by writing a formula.

5. In the Error Condition Formula section, enter the following (modified to fit your field names):

AND(Repairs_Suggested__c = TRUE,
ISBLANK(Suggested_Repairs__c))

The AND operator says that all of the parameters must be true to evaluate the formula and return a True result. It’s only when the result of the formula is evaluated to True that the validation error message will display to the user. Let me break this formula down into everyday language:

“When Repairs Suggested checkbox is checked, and the Suggested Repairs text box is blank, trigger the rule.”

After creating the formula, be sure to press the Check Syntax button. This will tell you if there are any errors in the formula.

NOTE: Check Syntax will not tell you if your formula is correct – only that the formatting of the formula is correct including the proper use of operators. You’ll still need to test the validation rule to ensure it’s working properly.

Validation Rule FormulaWith the formula created and validated via the Check Syntax button, it’s time to create the error message and determine the error message location.

6. In the Error Message section, enter the following into the Error Message field: Please enter a description of the suggested repairs.
7. The Error Location defaults to Top of Page. Let’s change it to Field and choose Suggested Repairs from the provided picklist.

Error Message

Whenever possible, I prefer to have the error message display on a specific field. The only time Top of Page should be selected is if you know that multiple fields need to be satisfied as part of the validation rule. In that case, the error message should be very descriptive and include the field names that require an update.

Now, if I go back to my vehicle repair and try to save the record, this is what the validation rule should look like:

Validation Rule Success

If you received this error message when updating one of your records, congratulations! You’ve just created a validation rule!

Validation Rule Tips & Tricks

Here is some helpful information on validation rules that may come in handy.

  • Validations can be created on any object.
  • Formulas are the only way to create a validation rule, but they are super powerful.
  • The Check Syntax button doesn’t validate your actual formula. Be sure to test it first!
  • Build your formula in a sandbox or dev org first. Otherwise, the rule may go live and impede the users workflow if things go wrong.
  • When testing the validation rule, don’t test only the requirements of the validation rule. I’ve built a rule and occasionally found that the formula worked in reverse or wasn’t strict enough.
  • Provide descriptive error messages. Users need to know how to fix the issue. Be sure the provided error message is descriptive enough to explain how to fix the issue.

Additional Resources

This was a fairly straightforward validation rule, but there are thousands of other validations that may be beneficial to your companies org. Here are some resources that I think will be helpful to you when evaluating how to write a validation rule, or provide some ideas on the types of validation rules to create.

  1. Help & Training – Validation Rules Examples
  2. Trailhead – Formula’s and Validations
  3. Help & Training – Formula Operators and Functions Overview

My good friend Shell Black makes these awesome YouTube videos, and he has done one on Validation Rules. Here’s his video that I think will also provide some additional insight.

How are you using validation rules in your organization? Leave me a comment below – I’d love to hear from you!

11 thoughts on “ Oh Snap! An Intro to Validation Rules ”

  1. Brent,
    Love all the Admin Hero content. All this great info has really help launch my Salesforce career. Thanks again and keep the great work.

    Like

  2. There is a typo in this paragraph:

    Whenever possible, I prefer to have the error message display on a specific field. The only time Top of Page should be selected is if you know that multiple fields need to be satisfied as part of the validation rule. In that case, the error message should be very description [descriptive] and include the field names that require an update.

    Thanks for putting this blog together.

    Like

  3. When you go to an object’s page you will see a related list there for Validation Rules. If you then select a field you will see a related list there for Validation Rules. What is the difference and which should be used when?

    Like

    1. I recall this being confusing for me as a new Admin. Some of the Setup navigation didn’t make sense right away. Either location would be acceptable to create or modify validation rules. By viewing the Validation Rules related list on the Object, you’ll see all Validation rules for that object. The related list at the field level would display rules where that field is part of the validation rule.

      Like

      1. I have read your post about VLOOKUP but I don’t think that this formula can help me. What I need to do is check if a field in another non related object contains a certain code so I have tried the CONTAINS formula but I get an error ”
        Error: Incorrect parameter type for function ‘CONTAINS()’. Expected Text, received Object
        “. I have tried both versions below
        CONTAINS(“AAA”, $ObjectType.MyObject__c.Fields.Myfield__c)
        CONTAINS(“AAA”, TEXT($ObjectType.MyObject__c.Fields.Myfield__c) )

        I also wrote on Salesforce Community but I got the answer that “you cannot do it using a validation rule. You will have to write a trigger(Apex code) to do something like this.”

        I will research more about pulling data down from one object to another.

        Thanks for your help

        Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.