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
Ashish Dixit 2Ashish Dixit 2 

i want handler class for my 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 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);
                    }
                }
            }
        }        
    }
}
 
FARSANA PSFARSANA PS
hi,

1.First create a helper class for your trigger.Trigger Helper class is the class which does all the processing for trigger.
public class TriggerHandler
 {
   public void OnBeforeInsert(list<PMO__c> triggerNew,map<Id,PMO__c> triggerNewmap)
       {
           // This is used to Call before Insert method.
        }
  public void OnAfterInsert(list<PMO__c> triggerNew,map<Id,PMO__c> triggerNewmap)
      {
           // This is used to Call afterInsert method
        }
   public void OnBeforeUpdate(list<PMO__c> triggerNew,map<Id,PMO__c> triggerNewmap,list<PMO__c> triggerOld,map<Id,PMO__c>       triggerOldmap)
       {
          // This is used to Call  before  update method.
      }
}

2.Update your trigger,so that Trigger will contain only the helper class method calls ,rest of the processing is done in helper class.

trigger PMO_Trigger on PMO__c (before insert,before update,after Insert)
{
 //Declaration of object ‘objHandler’.
   TriggerHandler objHandler = new TriggerHandler();
   if(trigger.isAfter && trigger.isInsert)
    {
      objHandler.OnAfterInsert(trigger.New, trigger.newMap);
    }
   if(trigger.isBefore && trigger.isInsert)
    {
      objHandler.OnBeforeInsert(trigger.New, trigger.newMap);
    }
    if(trigger.isBefore && trigger.isUpdate)
    {
    objHandler.OnBeforeUpdate(trigger.New, trigger.newMap,trigger.Old,trigger.oldMap);
     }
 }

Hope this will help..
refer:http://blog.mirketa.com/salesforce-developer-guide-use-of-helperhandler-class-to-manage-trigger-execution/
Ashish Dixit 2Ashish Dixit 2
Thank you so much for your valuable time  , i will try above given solution . Thanks a lot 
Ashish Dixit 2Ashish Dixit 2
I have tried but that did not works for me , I am new in apex coding ,Could you please provide the exact code of handler class for my above given trigger. thank you so much.