function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
GMASJGMASJ 

How to by pass trigger for a specific user

Hi, 

  How to by pass trigger for a specific users in salesforce. 

Thanks
Sudhir
Best Answer chosen by GMASJ
Jun JoseJun Jose
The easiest way is to use UserInfo.getName() method.
 
If (UserInfo.getName() != 'Some User Name') {
    // Do processing here
}

I would recommend to have this implemented by profile instead
 
If (UserInfo.getProfileId() != 'Certain Profile ID') {
    // Do processing here
}

But if you want to have support for multiple users, you should use a map or a set to make this more maintainable.
 
Set<ID> bannedIds = new Set<ID>();
bannedIds.add('ID1');
bannedIds.add('ID2'); // add more ids to ban

if ( ! bannedIds.contains(UserProfile.getProfileId()) ) {
   // do processing here
}


 

All Answers

Jun JoseJun Jose
The easiest way is to use UserInfo.getName() method.
 
If (UserInfo.getName() != 'Some User Name') {
    // Do processing here
}

I would recommend to have this implemented by profile instead
 
If (UserInfo.getProfileId() != 'Certain Profile ID') {
    // Do processing here
}

But if you want to have support for multiple users, you should use a map or a set to make this more maintainable.
 
Set<ID> bannedIds = new Set<ID>();
bannedIds.add('ID1');
bannedIds.add('ID2'); // add more ids to ban

if ( ! bannedIds.contains(UserProfile.getProfileId()) ) {
   // do processing here
}


 
This was selected as the best answer
Raj VakatiRaj Vakati
You can do this approach ...

Step 1 : - create a new custom filed on the user object. ( Bypass_Trigger__c) Data type is checkbox ..

If this checkbox is checked to any user the trigger wnt execute to the user ...

 
//  Query the salesforce current logged in user 
    User u=[Select Bypass_Trigger__c 
                       from User
                       where id=:UserInfo.getUserId()] ;
    // Check if the current user is allowed to bypass the trigger 
    if(!u.Bypass_Trigger__c){

// YOUR Loggic 

}

 
GMASJGMASJ
Thanks for suggestion it worked. 
Mila JohnsonMila Johnson
Hi,

Im trying to do something similar in my org. Bypass a specific user for the account trigger, getting the following error.

Variable does not exist: Bypass__c

This is my code:
trigger Deleteaccount on Account (before insert) {    
    //  Query the salesforce current logged in user 
    User u=[Select Bypass__c
                       from User
                       where id=:UserInfo.getUserId()] ;
    // Check if the current user is allowed to bypass the trigger 
    if(!u.Bypass__c){
        
      for(Account acc : trigger.old){
          if(Bypass__c == 'true'){
            acc.adderror('Account Cannot be deleted');
    }
}
}
}

Thank yoU!