Tuesday, April 30, 2019

Custom settings to enable disable salesforce validation rules

salesforce lightning,

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:


No comments:

Post a Comment