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:
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?
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?
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…
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
- Create the Custom Permission
- Add the reference to your validation rule criteria
- Assign to profiles or permission sets
- Deploy successfully, go home, and enjoy a nice hot cocoa. You earned it!
One thought on “Use Custom Permissions To Bypass Validation Rules And Pass Unit Tests”
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.