In our system, we have recently added a validation rule, which works fine with the actual usage but most of our existing test code doesn't comply with that rule when creating test data. Since test.isRunningTest() does not work with validation rule, is it possible to auto-deactivate it when running tests?


It's impossible to just run some code to bypass validation rules. However, there are techniques for validation rules that are under your control. One such article is here (not mine). The general technique is to create a custom setting, and use that custom setting to bypass validation rules by building it into the validation rules:

AND($Setting.Special__c.Enforce_Validation__c, ...)

Of course, this won't help you if you're in a managed package and you're trying to worry about someone else's validation rules, but this method can allow in-house code to bypass validation rules with very little extra effort.

  • Thank you for this. It taught me a lesson of the importance of having a test utility class as well. – Lance Shi Jul 23 '14 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.