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
VPM 1VPM 1 

How to convert a trigger to helper class

I have created a trigger which creates an Individual, ConsentPointEmail and ConsentPointConsent records whenever email address is changed on a Person account. Have done the same thing for Phone number change. I wanted to check if I can convert that trigger to a helper class. Below is my trigger code: 

trigger consentAccountTrigger on Account (after update) {

string IndId;
string accPersonEmail;
string accPhone;    

List<Individual> IcList = new list<Individual>();
List<ContactPointEmail> cpeList = new list <ContactPointEmail> ();
List<Account> acc1List = new list <Account> ();   
List<ContactPointConsent> cpcList = new list <ContactPointConsent> ();
List<ContactPointPhone> cppList = new list <ContactPointPhone> ();
   
for (Account acc: Trigger.new) {
system.debug ('inforloop');
      if( trigger.oldMap.get(acc.Id).PersonEmail != acc.PersonEmail && acc.IsPersonAccount == TRUE && acc.PersonIndividualId == null){      
          system.debug ('checkIndividual');   
          Individual Ic = new Individual();
          Ic.LastName= acc.LastName;
          Ic.FirstName = acc.FirstName;  
          IcList.add(Ic);

}else if (acc.PersonIndividualId != null){
          IndId = acc.PersonIndividualId;                 
}

 }

    insert IcList;

for (Account acc1: Trigger.new) {
accPersonEmail = acc1.PersonEmail;
accPhone = acc1.Phone;    
if(IcList!=null && !IcList.isEmpty()){
for (Individual Ic1 : IcList){
    if(accountRecurssionHandler.avoidrecussion == TRUE){               
       accountRecurssionHandler.avoidrecussion = FALSE;
       Account acc2 = new Account ();
       acc2.id = acc1.Id;
       acc2.PersonIndividualId = Ic1.id;
       IndId = Ic1.id;
       acc1List.add(acc2);

}

if(trigger.oldMap.get(acc1.Id).PersonEmail != acc1.PersonEmail){
              
     ContactPointEmail cpe = new ContactPointEmail();
     cpe.IsPrimary = True;
     cpe.EmailAddress = accPersonEmail;
     cpe.ParentId = Ic1.Id;   
     cpeList.add(cpe);

 }             
 }

 }else{                                     
if(trigger.oldMap.get(acc1.Id).PersonEmail != acc1.PersonEmail){

 //if(acc.LastName == IcLastName){               
ContactPointEmail cpe = new ContactPointEmail();
cpe.IsPrimary = True;
cpe.EmailAddress = accPersonEmail;
cpe.ParentId = IndId;   
 cpeList.add(cpe);

 //}

}
}                                                             

    }

        update acc1List;                                      
        insert cpeList;   

    

        for (Account acc3: Trigger.new) {
        for (ContactPointEmail cpe1 : cpeList){  
            if(acc3.PersonEmail == cpe1.EmailAddress){
               ContactPointConsent cpc = new ContactPointConsent();
               cpc.Name = acc3.PersonEmail;
               cpc.ContactPointId = cpe1.Id;
               cpcList.add(cpc);                          
}
}         

        }

        //insert cpcList;

        
        for (Account acc: Trigger.new) {
          system.debug ('inforloop');
          if( trigger.oldMap.get(acc.Id).Phone != acc.Phone && acc.IsPersonAccount == TRUE && acc.PersonIndividualId == null){      
          system.debug ('checkIndividual');   
          Individual Ic = new Individual();
          Ic.LastName= acc.LastName;
          Ic.FirstName = acc.FirstName;  
          IcList.add(Ic);

}else if (acc.PersonIndividualId != null){
          IndId = acc.PersonIndividualId;                 
}

 }

    insert IcList;

for (Account acc1: Trigger.new) {
accPhone = acc1.Phone;    
if(IcList!=null && !IcList.isEmpty()){
for (Individual Ic1 : IcList){
    if(accountRecurssionHandler.avoidrecussion == TRUE){               
       accountRecurssionHandler.avoidrecussion = FALSE;
       Account acc2 = new Account ();
       acc2.id = acc1.Id;
       acc2.PersonIndividualId = Ic1.id;
       IndId = Ic1.id;
       acc1List.add(acc2);

}

if(trigger.oldMap.get(acc1.Id).Phone != acc1.Phone){
              
     ContactPointPhone cpp = new ContactPointPhone();
     cpp.IsPrimary = True;
     cpp.TelephoneNumber = accPhone;
     cpp.ParentId = Ic1.Id;   
     cppList.add(cpp);

 }             
 }

 }else{                                     
if(trigger.oldMap.get(acc1.Id).Phone != acc1.Phone){

 //if(acc.LastName == IcLastName){               
ContactPointPhone cpp = new ContactPointPhone();
cpp.IsPrimary = True;
cpp.TelephoneNumber = accPhone;
cpp.ParentId = IndId;   
 cppList.add(cpp);

 //}

}
}                                                             

    }

        update acc1List;                                      
        insert cppList;   

    

        for (Account acc3: Trigger.new) {
        for (ContactPointPhone cpp1 : cppList){  
            if(acc3.Phone == cpp1.TelephoneNumber){
               ContactPointConsent cpc = new ContactPointConsent();
               cpc.Name = acc3.Phone;
               cpc.ContactPointId = cpp1.Id;
               cpcList.add(cpc);                          
}
}         

        }

        insert cpcList;

}
SwethaSwetha (Salesforce Developers) 
HI VPM,
I recommend reviewing the Apex Trigger Design Pattern example listed in http://forceforte.com/convert-trigger-to-helper-class-and-trigger/ to get started.

The idea is to create your helper class with the logic from your trigger. Then, you create one single Trigger for your Account object, using the trigger variables to define which methods runs for which context:

Other examples:
https://developer.salesforce.com/forums/?id=906F0000000kIqgIAE
https://salesforce.stackexchange.com/questions/66104/converting-trigger-into-a-helper-class-trigger-dispatcher/66108

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you