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