+ Start a Discussion
Subodh Shukla 16Subodh Shukla 16 

How to create an handler class for following trigger code

Hi All,

i am new in apex code i write a trigger and dont have idea about the handler class so i directly write logic in trigger itself. it would be great if anybody let me know how to write handler class and call this trigger code. 
trigger Lookupuserrecord on Account (before insert, before update) {
        
        Set<Id> ParentIds = New Set<Id>();
        For(Account Acc : Trigger.New)
    {
        ParentIds.Add(Acc.Master__c);
    }
       List<Master__c> ParentList = [Select Id,test_user1__c from Master__c where id =: ParentIds];
       
       For(Account CO : Trigger.New)
    {    
        For(Master__c PA : ParentList)
        {
            IF(CO.Master__c == PA.ID)
            {
                IF(PA.test_user1__c != NULL)
                {
                    CO.test_user1__c = PA.test_user1__c ;
                }
            }
        }
    }    
       
        
}

 
Best Answer chosen by Subodh Shukla 16
Arvind KumarArvind Kumar
Hi Subodh,

I have written trigger & handler class for your scenario. It will work fine for bulk records. 

Please use it.

Prithviraj, You have written a good code.

Trigger:

    trigger AccountTrigger on Account(before insert, before update)  
    {
        AccountTriggerHandler handler = new  AccountTriggerHandler();
        
        handler.updateUser(Trigger.new);  // Call method for update account field.

    }

Trigger Handler Class:

    public class AccountTriggerHandler
    {
           public void updateUser(List<Account> accList)
           {
                  Set<Id> masterIds = new Set<Id>();
                  
                  for(Account accObj : accList)
                  {
                    if(accObj.Master__c != null)
                       masterIds.add(accObj.Master__c);
                  }
            
                    Map<Id,Master__c> masterMap = new Map<Id,Master__c>([Select Id, test_user1__c from Master__c where Id IN : masterIds]);
                     
                     
                  for(Account acct : accList)
                  {
            
                    if(masterMap.containsKey(acct.Master__c))
                    {
                       Master__c masterObj = masterMap.get(acct.Master__c);
                  
                        if(acct.test_user1__c == null)
                
                           acct.test_user1__c = masterObj.test_user1__c;
                    }
                  }
           }
    } 

If you have any issue, you can write a message on this answer.

Thanks,
Arvind Kumar

All Answers

Prithviraj_ChavanPrithviraj_Chavan
Hi Subodh,
trigger Lookupuserrecord on Account (before insert, before update) {
        OnAccountTriggerHandler.MyMethod();
}

public class OnAccountTriggerHandler{
    public static void MyMethod(){
        Set<Id> ParentIds = New Set<Id>();
        For(Account Acc : Trigger.New)
        {
            ParentIds.Add(Acc.Master__c);
        }
        List<Master__c> ParentList = [Select Id,test_user1__c from Master__c where id =: ParentIds];
        
        For(Account CO : Trigger.New)
        {    
            For(Master__c PA : ParentList)
            {
                IF(CO.Master__c == PA.ID)
                {
                    IF(PA.test_user1__c != NULL)
                    {
                        CO.test_user1__c = PA.test_user1__c ;
                    }
                }
            }
        }    
    }
}

 
Arvind KumarArvind Kumar
Hi Subodh,

I have written trigger & handler class for your scenario. It will work fine for bulk records. 

Please use it.

Prithviraj, You have written a good code.

Trigger:

    trigger AccountTrigger on Account(before insert, before update)  
    {
        AccountTriggerHandler handler = new  AccountTriggerHandler();
        
        handler.updateUser(Trigger.new);  // Call method for update account field.

    }

Trigger Handler Class:

    public class AccountTriggerHandler
    {
           public void updateUser(List<Account> accList)
           {
                  Set<Id> masterIds = new Set<Id>();
                  
                  for(Account accObj : accList)
                  {
                    if(accObj.Master__c != null)
                       masterIds.add(accObj.Master__c);
                  }
            
                    Map<Id,Master__c> masterMap = new Map<Id,Master__c>([Select Id, test_user1__c from Master__c where Id IN : masterIds]);
                     
                     
                  for(Account acct : accList)
                  {
            
                    if(masterMap.containsKey(acct.Master__c))
                    {
                       Master__c masterObj = masterMap.get(acct.Master__c);
                  
                        if(acct.test_user1__c == null)
                
                           acct.test_user1__c = masterObj.test_user1__c;
                    }
                  }
           }
    } 

If you have any issue, you can write a message on this answer.

Thanks,
Arvind Kumar
This was selected as the best answer
Ashish Dixit 2Ashish Dixit 2
Hi All,

i am new in apex code, I write a trigger and dont have idea about the handler class so i directly write logic in trigger itself. it would be great if anybody let me know how to write handler class and call below trigger code. 
trigger PMO_Trigger on PMO__c (before insert,before update,after Insert){
    
    public static Set<Id> existingOppIds=new Set<Id>();
    public List<PMO__c> fmiList = new List<PMO__c>();
    if(Trigger.isBefore && Trigger.isInsert){
    /*********************Implement different access to the different users**************/
        PMO_ModuleSecurity.UpdateAccess(trigger.new);
     /**********************Limit the creation of PMO Module to only 1 module per opportunity******/ 
        existingOppIds =  PMO_ModuleSecurity.CheckNoOfPMOModule(trigger.new);
        if(existingOppIds.size()>0){                  
            for(Integer i = 0; i < Trigger.new.size(); i++){                
                if(existingOppIds.contains(Trigger.new[i].JJ_Opportunity__c)){
                  //check for existing opportunity 
                    Trigger.new[i].addError('Another PMO module already exists for this opportunity. Only 1 PMO module is allowed per opportunity.');
                }
                else{
                   // existingOppIds.add( Trigger.new[i].opportunity__c);
                }
            }
        }        
    }    
    /******************************* Grant PMOModule Access ******************************/
    if(  Trigger.isInsert && Trigger.isAfter){
        new AccessManager().grantAccess(Trigger.new);
    }     
    if(Trigger.isBefore && Trigger.isUpdate){
    /**********************Limit the creation of PMO Module to only 1 module per opportunity******/ 
        for(PMO__c fmi : Trigger.new){
            PMO__c oldOpportunity = Trigger.oldMap.get(fmi.id);
            if(fmi.JJ_Opportunity__c != oldOpportunity.JJ_Opportunity__c ){
                fmiList.add(fmi);
            }
        }
        if(fmiList.size()>0){
            existingOppIds =  PMO_ModuleSecurity.CheckNoOfPMOModule(trigger.new);
            if(existingOppIds.size()>0){
               for(Integer i = 0; i < Trigger.new.size(); i++){                
                    if(existingOppIds.contains(Trigger.new[i].JJ_Opportunity__c)){
                    //check for existing opportunity 
                    Trigger.new[i].addError('Another PMO module already exists for this opportunity. Only 1 PMO module is allowed per opportunity.');
                    }
                    else{
                        existingOppIds.add( Trigger.new[i].JJ_Opportunity__c);
                    }
                }
            }
        }        
    }
}