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!
…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 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!