Site icon Sunshine and Other Unhandled Exceptions

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:

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?

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.

How to bypass Validation Rules with Custom Permissions

  1. Create the Custom Permission
  2. Add the reference to your validation rule criteria

  3. Assign to profiles or permission sets


  4. Deploy successfully, go home, and enjoy a nice hot cocoa. You earned it!

Cheers!

Exit mobile version