The best way to achieve that is to create a Chexbox on the User Sobject. You will name that Checkbox something like : "Bypass Triggers And Validations". The idea is, every time you want to bypass Triggers and Validations for a specific user, you just need to check that Checkbox on that User record. Then in all your Validations Rule, add the condition :
IF($User.BypassTriggerAndValidations, false,... your validation rule here...)
And in your Triggers add this line on top :
// If the byPassTriggers() is true, "return" will be called and then exit the Trigger
if(CurrentUser.byPassTriggers() == true) {
return;
}
CurrentUser is a class that you need to create, and will look like :
// CurrentUser load the current User, with the field BypassTriggersAndValidations__c, and return it.
public class CurrentUser {
private static User record;
public static Boolean byPassTriggers() {
if(record == null) {
record = [
SELECT BypassTriggersAndValidations__c
FROM User
WHERE Id =:UserInfo.getUserId()
];
}
return record[0].BypassTriggersAndValidations__c;
}
}
You can disable the validation rule and trigger by user's information or user's profile.Here are the working examples and please have fun with them.User information - Name: Test User (Id: 0050000001AabCD)
- Profile: TestProfile (Profile Id: 00e000000098XYZ)
IF($User.Id='0050000001AabCD',false,...the original validation rule...)
Example 1 of trigger (UserId as the criteria)
Trigger CaseTrigger on Case (before insert){
for(Case c : Trigger.new){
if(Userinfo.getUserId()=='0050000001AabCD') return;
... original trigger codes ...
}
}
Example 2 of validation rule (Profile as the criteria)
IF($User.ProfileId='00e000000098XYZ',false,...the original validation rule...)
Example 2 of trigger (Profile as the criteria)
Trigger CaseTrigger on Case (before insert){
for(Case c : Trigger.new){
if(Userinfo.getProfileId()=='00e000000098XYZ') return;
... original trigger codes ...
}
}
3 answers