How to disable/enable all validation rules for data loading

While working on a recruiting application, I found a solution for being able to load data into a SalesForce application without being blocked by validation rules.

Validation rules are usually intended to be applied only when a user creates a record and not when data is being imported from an external database. In this recruiting application, candidate records go several stages in a sequence (1-lead, 2-phone, 3-applicant, 4-interview, 5-contract negotiation, etc.) and this validation rule prevented the import process from loading candidate records in a stage higher than lead.

So the solution was to create a Custom Setting of type Hierarchy with a flag/checkbox in it that disables validation rules for a given user or profile. That is, all the validation rules will include this flag and only apply when the value of this flag is enabled.

To implement it:

1) click Setup, then on the left side, click App Setup/Develop/Custom Settings.

2) click New and create your settings as hierarchy/public

3) now create a custom field of type Checkbox:  click New, select Checkbox, click Next, type the name of the field as “Disable Validation Rules”, default to Unchecked, click Next, then click Save.

4) in the Custom Setting Definition/Your App Settings screen, click Manage to start configuring the setting you just created.

5) click the New button above “Default Organization Level Value”, leave the checkbox “Disable Validation Rules” unchecked and then click Save.

6) click “Back to List”, click the New button at the bottom, select Profile or User then choose the user id or profile that will perform the data loading, then click “Disable Validation Rules” and click Save.

7) now edit the validation rule appending the expression (highlighted below)
&& NOT( $Setup.Your_App_Settings__c.Disable_Validation_Rules__c )

  8 ) click Save

9) now the validation rule will only apply if the setting checkbox
Disable Validation Rules is unchecked for your profile/user

10) you can now load data freely and then, later, re-enable all
validation rules for your profile/user by changing the custom
setting.

11) you can use this way of implementing Custom Settings on
triggers too, just use the syntax below:

 Your_App_Settings__c s = 
 Your_App_Settings__c.getInstance( UserInfo.GetUserID() ); //or Profile
 if( s.Disable_Validation_Rules__c ) return; // skip trigger...

Pertinent Articles:
– Accessing Custom Settings
Let’s make some magic…


Posted

in

,

by

Tags:

Comments

10 responses to “How to disable/enable all validation rules for data loading”

  1. Krish avatar
    Krish

    Hey, I am using your method to by pass all the triggers and validation rules during data loading atleast to avoid the post data clean up process. Can i just fire one trigger and by pass remaining all during this process.. If so, could you please explain me how?

    1. Fernando F avatar

      Do you mean only fire the trigger on the first record and allow others?
      If so, you will have to add code to the trigger to ignore the custom setting on the first record and apply it to the rest.
      The details of that will depend on what the trigger is doing.

  2. Krish avatar
    Krish

    Hey its working perfect. Could you please let me know how to take care if have created a profile. It’s the same code applicable or do i need to change the code a lil bit..

  3. Sheila Murphy avatar
    Sheila Murphy

    Will this work when trying to deploy a new trigger and class into Production? i have to deactivate a lot of validation rules just to deploy.

    1. Fernando F avatar

      Yes, it will work if you add this mechanism to each trigger/validation rule.

      However, the best practice is to change your test classes to provide valid values so the validation rules pass during deployment.

      For example, if you are deploying a Lead trigger and there is a validation rule requiring the Lead Company to be a certain value, you can code the test class to create a Lead and populate the Company with a valid test value.
      That way you don’t have to do anything else to make the trigger/class pass Production deployment.

  4. Johna431 avatar
    Johna431

    I relish, cause I discovered just what I used to be taking a look for. You’ve ended my 4 day lengthy hunt! God Bless you man. Have a great day. Bye eddebffecdfc

    1. Fernando F avatar

      I am glad that it has helped someone!
      How did you find the article?
      If I could make it more visible perhaps others will find it sooner and benefit.

  5. Jancy Mary avatar
    Jancy Mary

    Hi Fernando,

    I went through this complete article & it seems to be very helpful, I’m working on a similar scenario where I want to bypass certain set of Managed package triggers as they throw CPU timeout error when I try to update Thousands of Contacts using Data Loader.

    SFDC Support team finalized the root cause for this issue as “Code is not fully Optimized on some triggers”, I can’t deactivate the triggers as these are from a managed package.

    So, to go with custom settings should I add the trigger logic (the logic which you mentioned on this block to implement custom settings on triggers) in some way if I can edit the trigger.

    Or please suggest me if there are any other ways to over come the issue. Your help on this is much appreciated.

    Thanks,
    Jancy Mary

    1. Fernando F avatar

      Hi Jancy,

      Unfortunately, I believe there is no way to apply this idea to a managed package trigger from the perspective of the user of the package.

      I can only see 3 alternatives:

      1) reduce the number of records updated so that the trigger can process them without the CPU timeout. That is, reduce the batch size in your update to slow things down so that the unoptimized trigger can at least complete each operation. Here is how to reduce the batch size on the Data Loader: https://help.salesforce.com/HTViewHelpDoc?id=configuring_the_data_loader.htm

      2) ask the owner/publisher of the managed package to update the trigger and implement a way for the user to disable it. That is, he could implement this custom setting idea in his triggers and re-release the package.

      3) uninstall the package and let the owner/publisher know the reason was that their triggers were not optimized for your specific case. You can re-install it later or try a different package.

  6. unidha avatar
    unidha

    I am trying your method, it works successfully.But I need to set the custom setting back to false so I put it as the end of trigger.It didn’t work and fire back validation rules.How to revert back the bypass validation rule after you done the trigger?