Saturday 28 January 2017

How to disable/enable all validation rules for data loading in Salesforce

How to disable/enable all validation rules for data loading in Salesforce:



1) click Setup, then on the left side, click 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() );
  • if( s.Disable_Validation_Rules__c ) return;

Examples : 

In Trigger : 

trigger AccountTriggerHandler on Account (before insert,before update,after update) {
    Application__c bypassTrigger= Application__c.getInstance( UserInfo.getUserID() );
    if(!bypassTrigger.DisableValidations__c){
        if(trigger.isbefore && trigger.isinsert){
            for(account acc : trigger.new){
                if(acc.phone == null){
                    acc.phone.addError('phone no required');
                }
            }
        }
        
    }
    
}

Validation :

ISBLANK( Phone ) && NOT( Setup.Application__c.DisableValidations__c )


Complete Salesforce CPQ training Free videos

Salesforcestart:: We are excited to announce that our YouTube channel, Salesforcestart, is your one-stop-shop for all things Salesforce CPQ!...