Ignoring Validation rules when deploying code

I have seen a solution that uses a Custom Setting of ValidationRuleEnabled.

ALL validation rules set up have the && $Setup.CustomSetting__c.ValidationRuleEnabled__c added.

When you want to deploy any code then the administrator changes the Custom Setting to FALSE, deploy the new code; don't forget to re-enable the Custom Setting!

Again this is not ideal as the 'legacy' code should be updated to accommodate the new validation rules; ideally at the time of creating the new validation rules (but who checks code coverage after making a small change like a validation rule?)


The use of $Setup.CustomSetting_c.ValidationRuleEnabled_c is very creative.

I typically handle this by making sure that any records I create using "UNITTEST" in the Name field and then check for NAME != "UNITTEST" in the validation rule.

The issue with legacy code still exists. Ideally, Salesforce.com will come up with a simple function for ISUNITTEST().


I vote for #1 - update all existing code written by other people, etc. Why? It's very clean and makes sure that the code still works with those validations in place or not. What happens if you have a validation rule that requires a field if X,Y,Z and your Apex code had not previously assumed this to be true. In fact what if your Apex code assumed that the field would be blank if X,Y,Z? If you bypass the validation rule the Apex code would still pass it's tests, but in production when the field was filled in when X,Y,Z the Apex code would break.

Edge case to be sure and for the most part we all agree that it's painful when new validation rules break your or past tests so I'd bet most people do #2 (disable it) and then when they have time go back and do #1 (clean test code).

One thing that can really really help is if you create a 'TestDataGenerator' class that is responsible for creating all of your test data, then you only need to modify the code there as opposed to everywhere. Of course this doesn't help for your unmanaged packages installed off the AppExchange, but at least helps you with your code.

If you really want to bypass the validation rules what I typically do is check the user's profile. Typically you can set it up so System Admin or a custom profile "Apex Admin" doesn't have to have the validation apply. Much like the CustomSetting, however if you use the custom setting during the deployment no user has the validation apply whereas with a check to profile only the user(s) with that profile can bypass the validation.

Looking forward to hearing more folk's thoughts on this one!