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!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s