Admins

Your Complete Guide to Validation Rules in Salesforce

Lucy Mazalon

By Lucy Mazalon

Validation Rules in Salesforce ensure that users can only save records when the data entered meets specific criteria. Salesforce Admins define these rules using logical conditions that evaluate to either “true” or “false”. If a rule evaluates to “true”, the record can’t be saved, and Salesforce displays a custom error message explaining what needs to be corrected before the user can try again.

If that sounds a little technical, don’t worry. In this guide, you’ll learn what validation rules are, when to use them, how to create them, and how to work with more advanced concepts like syntax and bypass logic.

What Is a Validation Rule?

A validation rule is a way to check that the data being entered or updated on a record meets specific criteria before Salesforce allows it to be saved. Think of it as a gatekeeper: if the data doesn’t meet the standard you’ve set, the record gets blocked, and the user sees an error message explaining what needs fixing.

Salesforce caps the number of active validation rules per org, depending on your edition.

When Should I Use Validation Rules?

Use validation rules to maintain user input data or system modified records.

  • User input data: Where users may be prone to making mistakes or cutting corners, or when you need data to be in a certain format. 
  • System modified records: Changes to data triggered by imports, automated processes, integrated systems, etc. will need to abide by validation rules*.

Having said that, validation rules are a safety net that these processes, ideally, should not bump up against. Admins should prepare/design automation/integrate systems respecting active validation rules. As we will see later, you can also write validation rules to bypass system modified records.      

The most famous example is “Closed Lost Reason” on Opportunities. Imagine that you want to track why potential sales fell through. Your sales reps change the Opportunity Stage to “Closed Lost” but forget to select a reason – stop right there! The rep is prompted to fill in the field and then saves the record successfully.

A validation rule saves the day by preventing reps from closing an Opportunity while the “Closed Lost Reason” field is blank.

READ MORE: Beyond Validation Rules: A Guide to Making Things Required in Salesforce

Keep in mind that validation rules are limited to the object they’re built on. If your requirement involves checking related or cross-object records, that’s outside what a validation rule can do declaratively. Consider checking out Flow’s Custom Error component, which can help you handle those more advanced scenarios.

Validation Rules vs. Required Fields

A question you might ask is, “Why not simply make the field required?” Required fields are ‘all or nothing’; you must complete the field for the record to be created. This creates a stricter but broader rule, while you can customize validation rules to kick in only under certain conditions.

In the Closed Lost Reason example above, we don’t require that field to be completed until the Opportunity Stage is being changed to “Closed Lost”. It wouldn’t make sense for the ‘Closed Lost Reason’ field to be required at any other stage of the pipeline. The ‘Stage’ value is the condition. 

How to Create a Validation Rule in Salesforce

Validation rules can be broken down into three parts: 

  1. Name and Description
  2. The Rule (the Error Condition Formula)
  3. The Error Message

Let’s see how the ‘Closed Lost Reason’ validation rule example looks from an admin’s perspective. 

1. Name and Description

A clear name and description is very important – a small part of your org documentation that will benefit others who want to understand the purpose of the rule, and/or to modify it in the future. 

Keep it short and simple! You may find that the description could be the same as the error message (step #3), but also add any context about who the rule was requested by, or what data issues the rule is preventing.

2. The Rule (the Error Condition Formula)

Setting up the validation rule itself is the trickiest part of this process to grasp. Even though I’ve worked with the Salesforce platform for years, I still need to refer back to validation rule reference guides (hopefully you, too, will bookmark this page!).

“Error Condition Formula” is the fancy name for the rule. Here is where you will tell Salesforce “if this happens | then that must be true“, for example:

“If the Opportunity ‘Stage’ is ‘Closed Lost’ | then the ‘Closed Lost Reason’ field must not be blank”

It’s easy to get confused when writing a validation rule formula, as they work differently compared to writing formula fields. This used to mess with my head, as validation rule formulas work backwards from what I’d expect. In formula fields, you’re writing the formula for what should happen, while in Validation Rules you’re writing the formula for what shouldn’t

If the formula comes back TRUE, that’s Salesforce saying “yep, this is the bad scenario, block it”. It’s the opposite instinct from writing a formula field, where TRUE usually means “yes, show this”.

Here’s a tour around each of the buttons above the formula box. 

Insert Field

This opens a pop-up that lists fields in the object you are writing the validation rule for.  

Tip: Use this button! Sure, it does save time, but also many validation rule errors are caused by field names being slightly different than you may have guessed.

Remember, what you see on the record is the “Field Label”; Validation Rules and other formulas use the “Field API Name“. 

In fact, Opportunity Stage is a perfect example. The API Name is “StageName”, not Stage:

Once you have located your field, use the “Insert” button to drop it into the formula.

Insert Operator

This opens a dropdown menu that lists operators, such as + (add), <> (not equal), etc.  

You will be familiar with most because they follow standard mathematics syntax.

The key ones you need to know, which will stitch your formula statements together, are: 

  • && (and)
  • || (or)

Insert Selected Function

This menu lists functions that you may be unfamiliar with.

There are different types of functions (view a full glossary here). Luckily, by selecting one, a description appears below the list. 

The most commonly used functions are:

  • ISBLANK(field) returns “True” if the field is blank.
  • ISPICKVAL(field, specific picklist value) returns “True” if a picklist value in a field matches the picklist value in the formula.
  • TEXT() converts a picklist value to text, a quirk for writing formulas that ask if a picklist field is blank/not blank.
  • AND() returns “True” if all the items or functions are “True”.
  • OR() returns “True” if any of the items or functions are “True”.

When to use && and ||, or AND() and OR()? We’ll come back to this later!

Check Syntax

This checks if the rule has been written correctly. Salesforce will even tell you what the problem is.

The most common issues are you:

  • Are missing a parenthesis “)” somewhere.
  • Misspelled a field name.
  • Used the wrong function.

3. The Error Message

This error message will appear when a user does not meet the requirements set out by the validation rule. This is how the admin explains what the user must do to correct the record, before clicking “Save” again. 

The error message location can appear at the top of the page or above the specific field causing the failed save. If there’s only one field the rule references, I recommend you locate the error message on the field (otherwise, locate it at the top of the page). 

Finally, activate your rule by checking the “Active” checkbox!

Top Tips for Working with Salesforce Validation Rules

Bypass Logic for Validation Rules

Ideally, admins should prepare/design automation/integrate systems respecting active validation rules. However, having a way to toggle which users should bypass validation rules is very useful.

Step 1: Create a checkbox field on the User object.

You can call it “bypassVR__c”, “overpassVR__c”, or something more meaningful to you.

Step 2: Edit the User records.

Check the checkbox field for all users who should avoid the validation rules. 

Step 3: Add the condition to validation rules. 

Enter the following into every validation rule you create in your org:

&& NOT(OverpassVR__c)

While this trick is pretty straightforward and manageable, there’s also another way to bypass Validation Rules using custom permissions and have them managed via permission sets and permission set groups. You can read more about that here.

There’s also the alternative of bypassing using Profiles, which is especially handy if the users you want to grant this to all belong to the same profiles. You can reference a user’s Profile directly in the formula, using the $Profile global variable. This is especially useful if you don’t want to manage an extra checkbox field!

For example, if the profile you want to grant bypass privilege to is named “Power User”, add this to your validation rule:

$Profile.Name <> "Power User"

This tells Salesforce that if the user’s Profile is “Power User,” skip this rule.

Using && and ||, or AND() and OR()

&& and AND() as well as || and OR(), can be used interchangeably. The difference lies in the syntax and readability. Here is the same formula, written in different ways: 


Basically, This && That  is the same as AND(This, That) the same way This || That is the same as OR(This, That). 

Which should you use? Answer: it’s mostly up to personal preference. However, if your rule contains multiple statements, you should use AND() and OR() because indenting and using separate lines will help identify syntax issues. && and || are typically used more as connectors between simpler expressions.

READ MORE: 7 Validation Rules to Get Your Salesforce Org AI-Ready

Summary

This guide has covered when to use validation rules and how to create them, as well as some tips for bypassing and common logic functions. 

Hopefully you now have a solid understanding of how validation rules work, including that “TRUE = block it” logic that confuses admins early on. From here, you’re ready to start applying these formulas to your own org’s requirements, and if you want to see them in action, the next step is exploring real-world examples across Sales and Service.

The Author

Lucy Mazalon

Lucy Mazalon

Lucy is the Finance Director at Salesforce Ben. She is a 10x certified Marketing Champion and founder of The DRIP.

Comments:

    Mathew Abraham
    November 10, 2021 5:30 pm
    Thanks for this. However the below statement is incorrect: “Salesforce Admins set up the rules as statements that must be ‘true’ for the record to be saved – in other words, the rule consists of ‘yes/no’ questions, and the answer must be ‘yes’ to all of them.” As far as I understand, it’s just the opposite! Meaning, if the formula is ‘True’ the validation throws the error, and not the other way around. Please correct me if I am wrong! Thanks
    Lucy Mazalon
    November 15, 2021 10:18 am
    You're totally right, thanks for pointing that out Mathew.
    Dee
    November 17, 2021 12:12 pm
    I can't think of an automation that comes to mind but I LOVE this idea and can already visualize the impressive looking dashboards this can yield. Thanks for the food for thought!
    Varun
    May 26, 2022 7:28 am
    Each Account can only have 1 Open Opportunity at a time. There can be as many Closed Opportunities as needed, but only 1 Open Opportunity. Can anyone tell me how to do that. Thanks in Advance
    Christine Marshall
    May 31, 2022 10:13 am
    Hi Varun, I would created a roll-up summary field on the Account object that shows the number of open Opportunities (you'll need to add a filer to your roll-up to only show open Opps). Then you can reference this new field on the Opportunity object. Your rule will be something like: Number_Of_Open_Opps__c >= 1 You'll need to add the correct field name. This rule will simply check if the number of open opps is equal to or greater than 1.
    Maha
    August 17, 2022 9:01 am
    13. Only One Voucher can be assigned to 1 Certification request. (Validation Rule)
    Terry
    September 28, 2022 9:40 pm
    Can validation rules not be added to Activity fields in Salesforce? We're trying to use this as a workaround for not having conditional picklists on profiles to prevent certain types of users from selecting certain picklist values.
    Andreea D
    September 30, 2022 11:17 pm
    Hello Terry! I'd say short answer is yes, the longer answer is that you can create a validation rule on either the Task or Event object. For example, I just created a random validation in my developer sandbox to prevent myself from choosing values which contain a certain word in the standard Subject field when creating an Event : CONTAINS(Subject, "Call") && $User.ProfileId = "theprofileIdyouwant". ` When building the validation (the example is still on the Event object btw), custom picklists created on the Activity object will also be available to choose from, so in my case I went with one I just created for this example to use instead of the standard Subject one : ISPICKVAL(Test_1__c,"2") && $User.ProfileId = "theprofileIdyouwant" Hope this clarifies your question!
    Mishel Naumoski
    October 04, 2022 2:15 pm
    Hey everyone, I've been trying to create a validation rule that triggers the error message whenever a certain "Type" of opportunity is selected (it's a picklist value; it has these picklist values: New Business, Cross Sell, Up Sell, Down Sell, Churn) I have a required field on these opportunities called "Subscription Length" that needs to have a value in order to be able to save the opportunity. However, I only want this rule to trigger the error when the user selects these picklist values: New Business, Cross Sell & Up Sell, because it doesn't make sense to add a value in the "Subscription Length" field if they select a "Churn" opportunity. I wrote this formula but it doesn't seem to be working and I can't figure out why. I'm pretty sure my logic is not correct, I just can't figure out what I'm doing wrong. Formula: AND( ISBLANK( Contract_Length__c ), ISPICKVAL( Type ,"New Business"), ISPICKVAL( Type ,"Cross Sell"), ISPICKVAL( Type ,"Up Sell"), true,false) *note: the "Contract_Length__c" field in the formula is the "Subscription Length" field mentioned above. Please help!
    Andreea D
    October 06, 2022 10:22 pm
    Hello Mishel, At first glance, it seems that your formula needs an OR() considering that by the sound of your use case the Type cannot simultaneously be New Business, Cross Sell and Up Sell, so it would be only one of them. Try the formula below in your validation rule and let me know how it goes :) AND( ISBLANK( Contract_Length__c ), OR(ISPICKVAL( Type ,”New Business”), ISPICKVAL( Type ,”Cross Sell”), ISPICKVAL( Type ,”Up Sell”)))
    Alex W
    October 25, 2022 2:54 pm
    Hello, What if you are trying to create an error condition formula that is trying to identify if the escalated checkbox is not check for a certain record type, then the user can not send a certain email template and will receive an error message for service cloud. If true, then user should be able to send email template with no error. I was thinking using the IF statement. Please advise.
    Mishel Naumoski
    December 27, 2022 12:18 pm
    Hey Andrea, Sorry it took a while to respond. So I tried out the formula you suggested above and it works like a charm! Thank you so much for your help!
    bhuvana
    March 25, 2023 7:38 am
    hello, in my org have account in two record types one business account and another one is personal, my requirement is business account has phone field is mandatory and personal account has no mandatory . this problem using to validation rule. how can solve this problem please advise
    Gsa Proxies
    November 05, 2023 9:08 pm
    I have noticed that fees for online degree experts tend to be an excellent value. For instance a full 4-year college Degree in Communication in the University of Phoenix Online consists of 60 credits at $515/credit or $30,900. Also American Intercontinental University Online makes available Bachelors of Business Administration with a full study course element of 180 units and a cost of $30,560. Online learning has made getting your diploma far more easy because you can easily earn your own degree through the comfort of your home and when you finish from work. Thanks for all other tips I have learned through the site.

Comments are closed.