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
Vivek KidambiVivek Kidambi 

bypass trigger

I have a class which automatically creates portal user. I also have a trigger on user which has a callout on any updates on user. I need to bypass this user trigger whenever the user is created through this automated process and trigger should happen normally whenever a user edit happens other than auto creation process. how can i achieve this? Static variable a good option?
Best Answer chosen by Vivek Kidambi
William LópezWilliam López
Hello Success Factors,

Yes I think the best way to go its with an static variable. I would say maybe an static MAP or a SET so you can decide whether an user need to be bypasses.

Something like:
 
public class TriggerHelper {

    Private static Set<String> bypassedUsers ;

    public static void generateSet(){
        bypassedUsers = new Set<String>();
        
        //Here you can add more logic, like get some users from a custom object or with an specific detail
    }
    
    public static void addUserToByPass(String UserId){
        
        if (bypassedUsers == null){
            generateSet();
        }
        
        bypassedUsers.add(UserId);
        
    }
    
    public static boolean doesUserNeedToBeByPassed(String UserId){
        if (bypassedUsers == null){
            generateSet();
        }
        
         return bypassedUsers.contains(UserId);       
    }   
    
}

Then in the class just save the user to be bypassed, since you are creating a user I recomend to use UserId not ID.
 
TriggerHelper.addUserToByPass('newuser@test.com');
In the trigger, just ask before running the logic:

 
if( !TriggerHelper.doesUserNeedToBeByPassed(usr.UserName) ){
              
  ///Do you regular tasks
  

  //Additionally you can add these in the triggers as a loop prevention:

     TriggerHelper.addUserToByPass(usr.UserName);

}
Please let me know how it goes

Regards,

​Don't forget to mark your thread as 'SOLVED' with the answer that best helps you. 








 

All Answers

William LópezWilliam López
Hello Success Factors,

Yes I think the best way to go its with an static variable. I would say maybe an static MAP or a SET so you can decide whether an user need to be bypasses.

Something like:
 
public class TriggerHelper {

    Private static Set<String> bypassedUsers ;

    public static void generateSet(){
        bypassedUsers = new Set<String>();
        
        //Here you can add more logic, like get some users from a custom object or with an specific detail
    }
    
    public static void addUserToByPass(String UserId){
        
        if (bypassedUsers == null){
            generateSet();
        }
        
        bypassedUsers.add(UserId);
        
    }
    
    public static boolean doesUserNeedToBeByPassed(String UserId){
        if (bypassedUsers == null){
            generateSet();
        }
        
         return bypassedUsers.contains(UserId);       
    }   
    
}

Then in the class just save the user to be bypassed, since you are creating a user I recomend to use UserId not ID.
 
TriggerHelper.addUserToByPass('newuser@test.com');
In the trigger, just ask before running the logic:

 
if( !TriggerHelper.doesUserNeedToBeByPassed(usr.UserName) ){
              
  ///Do you regular tasks
  

  //Additionally you can add these in the triggers as a loop prevention:

     TriggerHelper.addUserToByPass(usr.UserName);

}
Please let me know how it goes

Regards,

​Don't forget to mark your thread as 'SOLVED' with the answer that best helps you. 








 
This was selected as the best answer
Vivek KidambiVivek Kidambi
Thanks William. That really helped.