Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
when we want disable all the validationrules for a specific user on all the sobjects how we can achieve this give me some examples

and how to deactivate the triggers for a specific user
3 answers
  1. May 4, 2016, 5:24 AM
    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;

    }

    }

     
  2. May 4, 2016, 4:58 AM
    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)
    Example 1 of validation rule (UserId as the criteria)

    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 ...

    }

    }

0/9000