Validation Rules

What is a validation rule?

A validation rule will conditionally prevent records from being saved.

Why use validation rules?

Use validation rules to ensure that users enter valid data when creating or updating records.

[toggle title_open=”Close Example” title_closed=”Open Example” hide=”yes” border=”yes” style=”default” excerpt_length=”0″ read_more_text=”Read More” read_less_text=”Read Less” include_excerpt_html=”no”]When an opportunity stage is “Closed/Lost”, the rep must enter the field “Reason Lost”:

1-29-2013 4-06-14 PM

Note, “Reason Lost” is NOT required when the opportunity stage is any other value.  Therefore, making “Reason Lost” required on the page layout would not be appropriate.  The key benefit of validation rules is that they conditionally prevent records from being saved.

For more examples:
[sc:link id=https://help.salesforce.com/HTViewHelpDoc?id=fields_useful_field_validation_formulas.htm&language=en_US text=”Examples of Validation Rules”]
[Should / Long / Salesforce.com]

[/toggle]

Creating validation rules

Validation rules can be created on most standard and custom objects, and use the same syntax as formulas.

[toggle title_open=”Close Example” title_closed=”Open Example” hide=”yes” border=”yes” style=”default” excerpt_length=”0″ read_more_text=”Read More” read_less_text=”Read Less” include_excerpt_html=”no”]

To create the “Reason Lost” validation rule, I navigated to Setup –> Customize –> Opportunities –> Validation Rules.

1-29-2013 4-34-20 PM

And created the following rule:

1-29-2013 4-35-55 PM

This rule will only trigger when the stage name is “Closed Lost” and the field “Reason Lost” is not populated.

[/toggle]

Considerations

  • Validation rules will impact API usage, web-to-lead, and web-to-case submissions.  Make sure to structure your validation rules so that they will not unintentionally interfere with these operations (such as rejecting a web-to-lead submission).  It is possible to either add exceptions for users/profiles in the validation rule, or use [sc:link id=http://forcecertified.com/2012/02/25/exceptions-to-validation-and-workflow-rules-best-practices/ text=”custom settings in conjunction with validation rules”] (this second solution is likely more complex than what would be presented on the ADM201 exam).  In some scenarios, you may need to disable validation rules when importing or updating data, and reinstate them afterwards.
  • Too many validation rules can be frustrating from a user interface perspective, as the error is not presented until after the user attempts to save the record.  Make fields required when possible (as they are indicated as required in the user interface), and employ other usability features as appropriate (for instance, create a page layout section for information required when an opportunity is lost if more than 1 field is captured).
  • Some formula arguments (such as VLOOKUP) can only be used in validation rules.

27 thoughts on “Validation Rules”

  1. Hi john,

    I have a validation rule that “rate” cannot be lower than $25, now I want anyone with a director or above hierarchy to be able to insert ANY rate. How would I accomplish this? Thanks, Sid.

    1. Have your validation rule evaluate the $user global variable (e.g. look at the user profile). You’d also need to check that the value in that field has changed (priorvalue not equal to current value).

      Should look something like this (not exact syntax):

      $User.profile <> ‘profile allowed’ &&
      Amount > 25 &&
      PRIORVALUE(Amount) <> Amount

  2. I am trying to create a validation rule that doesn’t allow a value greater than the current value. My custom field is Tank__c, can anyone help me with the syntax? I tried ISCHANGED but run into errors after the “greater than or equal to” symbol….any help would be appreciated!

        1. Thanks John.

          I know that works, but just don’t understand why/how…

          I know what is validating, just don’t understand the flow for true or false

          1. Taking a second look at this – you actually don’t need the IF statement.

            You can just put in:

            Tank__c > PRIORVALUE(Tank__c)

            The validation rule will fire if the result of the formula equates to true.

      1. Spot on Carols, thanks. I was trying :
        PRIORVALUE(Tank__c) >= ABS(Tank__c)
        which requires a lesser tank value for ALL field updates on the record. Any explanation on how to narrow it down to a specific field rather than the entire record inputs? It’s apparently the IF operator. I never would have thought to use the true or false inputs on my own. All of this seems to make sense until I try the next field on my own : ) Thanks for the help.

  3. John,

    If I want to write a validation rule to prevent duplicates, but not based on Name… is that possible with VLOOKUP? (From what I see and experiment with, VLOOKUP is only based on name.)

    SITUATION:
    Do not create a record of ContactID (lookup field to Contacts) + Interest (from picklist, e.g. “Physics”).

    *So the VLOOKUP needs to reference all the past ContactIDs AND Interest picklist values to prevent a duplicate combination.
    * I thought of adding a formula field to create a unique ID (= ContactID + Interest)… but I don’t think I can reference that with a VLOOKUP since it’s not the Name field.

    –Michael

    1. By the way — I love your site. It’s the most helpful thing I’ve found.
      Taking ADM 201 on 12/30 (to fulfill a goal to take it this year). 🙂

      1. John,

        Thanks for your speedy reply. I did see that Success article (I actually attempted to figure it out before I messaged you).

        I appreciate your help.

        –Michael

        1. Sure!

          In your example you could also use a workflow to concatenate the contact id and interest and store that value in a text field marked as unique.

          You’re on the right track for vlookup but I’m not sure that it would work exactly the way that you want it to.

  4. In the first “Consideration” there’s a link for more information about using custom settings in conjunction with validation rules. In the target of this link, it says “Notice that Default Organization Level Values are not set by default. This must be set in order for the validation rule to work. Click Manage [some odd characters] > New. ”

    What are Default Organization Level Values? And, where are they set and why? (I’ve looked through Help & Training for this, but no luck. They don’t seem to have anything with OWD, which is the closest I was able to guess or find.)

    1. Here’s another link that may help: http://www.buanconsulting.com/the-custom-setting-every-administrator-needs/

      When you create a hierarchy based custom setting you need to set the default in order it to work correctly:

      “Set the organization default behavior by clicking the New button above the Default Organization Level Value; The values will default appropriately in this instance to show that the default behavior will be to run validation rules, so you can just save.
      Begin adding your exception profiles by clicking New”

      1. Thanks, John. This actually a whole new can of worms because I had/have no idea what a custom setting is. Ack and yikes! That link talks about “a new Validation Rule Exception custom setting object”. Huh? For expediency’s sake, how likely is it that this topic will be on the cert exam?

  5. Goal – Execs are being transferred leads that are not followed up on as their day to day tasks do not include managing leads. I want to remove the ability for someone to transfer them a lead BUT keep the ability for them to view reports/dashboards components on leads.

    How could this be achieved? Is it possible to create a Validation Rule that prevents the transfer of a lead when the new Owner ID equals that of one of the execs or their associated profile?

  6. I have two page layouts (two diff record types using it) with a field “Salary” present in only one of the layouts. If I create a validation rule say SALARY__c 0, it fires for the page layout where I have not included salary. How do I avoid this?

Leave a Reply