Use Custom Permissions To Bypass Validation Rules And Pass Unit Tests

Picture this… it’s already 9pm on deployment night (Friday night of course). You’ve pushed to three environments already and spent at least three hours resolving unit test failures. If all your tests pass this time, you can go home and enjoy a well deserved hot cocoa….

THEN THIS HAPPENS:

Unit Tests Fail

FIELD_CUSTOM_VALIDATION_EXCEPTION

Someone else deployed a Validation Rule that is causing almost half your unit tests to fail, effectively ruining your weekend and destroying all your dreams. Has this ever happened to you? Well you’re not alone…

What to do next?

thinking-2681494_960_720

Should you simply Deactivate it?

…No, it’s there for a reason!

Should you use Hierarchical Custom Settings to bypass the Validation Rule?

…Well it turns out because Custom Settings are data, they are not populated in unit tests unless the test specifically populates them, so although great for loading data or excluding certain users in the system, they won’t work for your unit test failures.

Should you specifically reference and exclude Profiles/Users in the Validation Rule?

…This is an option a lot of folks consider, but it can lead to some serious technical debt. What if the Users and Profiles change? What if there is a long list of Users and Profiles that need to be excluded? Ideally you should reference something that’s scalable and reusable.

Should you update all your unit tests to populate the required fields?

slap.png

No! It’s already 9pm and this needs to be deployed before you can go home and drink that sweet sweet cocoa. Plus you enjoy your sanity and would like to keep it.

Should you use a Custom Permission to bypass the Validation Rule?

Yes! Here’s why…

excited-2681489_960_720

Custom Permissions let you define access checks that can be assigned to users via permission sets or profiles, similar to how you assign user permissions and other access settings.

  • Custom Permissions allow you to keep your validation rules active while still bypassing them under certain circumstances.
  • Custom Permissions are metadata, not data, so they do not need to be populated by unit tests to work.
  • Custom Permissions only need to be referenced once in the validation rule and can be easily assigned to or removed from both profiles and individual users or special groups of users via permission sets.
  • Custom Permissions can be used to bypass a Validation Rule in three simple steps…

How to bypass Validation Rules with Custom Permissions

  1. Create the Custom Permission
    Perm1
  2. Add the reference to your validation rule criteria
    Perm2Perm3
    Perm4
  3. Assign to profiles or permission sets
    Perm5
    Perm6
    Perm7
  4. Deploy successfully, go home, and enjoy a nice hot cocoa. You earned it!
    Cocoa

Cheers!

Published by

Maya Peterson

Maya is a 3x Salesforce MVP, a 2x Dreamforce Speaker, and holds 16 Salesforce Certifications. She has over 7 years of experience on the Salesforce Platform delivering high value solutions for a variety of customers and clients ranging from small business to enterprise and spanning across multiple industries. She is passionate about growing her own skills as well as others. When she's not going full ‘force’ you’ll find her creating art, backpacking, cooking, or volunteering in her community.

One thought on “Use Custom Permissions To Bypass Validation Rules And Pass Unit Tests”

  1. I had a contractor we worked with a while back suggest custom settings for validation bypass purposes. Recently, when setting up some custom permissions for something else, I had a suspicion this might be a better option due to validations being off for everyone during the period we turned off the setting, but wasn’t sure if I was maybe not considering a reason not to use them.

    This seems to confirm my suspicions for my reason, and a few others. Welp. I’ve got a bunch of validation rules to update haha.

Leave a Reply