Is it possible to disable the Apex trigger when a particular user logged in? Or Is any possibility to restrict the Apex trigger execution depends on user. Ex. My trigger should execute with User 1 and shouldn't execute with user 2 login (both users meets the criteria to execute trigger)


UserInfo.getUserName() give current logged in uesr name. Refer here.

You can put a check on trigger and verify with user name:

trigger MyTrigger  on MyObject (after insert,after update,before insert,before update) {
    if( UserInfo.getUserName() != '<User Name with which you don't want to run the trigger>'){
        //Do something
  • 4
    +1 The only downside to this is that users can change their user name. A more elegant solution might use Custom Settings, Custom Metadata, or Custom Permissions to determine if the user should execute the trigger. These options allow for changes to whom the trigger executes for without code deployments. – sfdcfox Dec 7 '19 at 16:27

We can implement this scenario by using custom setting as below :

trigger CustTrigger  on Custom_Object__c (after insert,after update,before insert,before update) {

    //Fetch custom setting containing restricted user ids
    MySettings__c restrictedUserIds = MySettings__c.getValues('Restricted_User_IDs');

    Set<String> restrictedUsers = new Set<String>();

    if(restrictedUserIds != null && String.isNotBlank(restrictedUserIds.value__c) && restrictedUserIds.value__c.contains(';')){
        //Ids can be seperated by semi-colon
        restrictedUsers = restrictedUserIds.value__c.split(';');

    //Allow trigger execution if custom setting is not maintained or user id not found in custom setting.
    if(restrictedUsers.size() == 0 || (restrictedUsers.size() > 0 && !restrictedUsers.contains(UserInfo.getUserId()))){

        //Trigger execution 


This approach has just one drawback of maintaining user ids in custom settings but since user ids will remain constant , any other changes to user object will not affect this logic.

Hope it helps.

Edit : As suggested by @DavidReed , we can maintain hierarchy custom setting for each user and allow execution of trigger only if it exists.

  • 1
    If you're going to use a Custom Setting, it's probably more maintainable just to use a Hierarchy Custom Setting that can be set at the level of the individual user. – David Reed Dec 7 '19 at 17:54
  • @DavidReed , Yes , i agree with your point.Hierarchy Custom setting will be more maintainable. Thank you for the suggestion. – Rohit C Dec 7 '19 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.