+ Start a Discussion
roni shoreroni shore 

Hi, I need help to create a handler class for following trigger

trigger AccountMergeTrigger on Account (after delete) {
        List<Account_Merge_History__c> listAccountBackup = new List<Account_Merge_History__c>();
            for(Account acct : trigger.Old) {
            if(String.isNotBlank(acct.MasterRecordId)) {    
            listAccountBackup.add(new Account_Merge_History__c(Name = acct.Name,ma_a__c =acct.MasterRecordId, ma_b__c = acct.Id, ma_e__c = acct.Id, ma_f__c = acct.MasterRecordId ));
        }
            }
        if(listAccountBackup.size() > 0) {
               insert listAccountBackup;
    }    
}

Please suggest
Best Answer chosen by roni shore
sfdcMonkey.comsfdcMonkey.com
Hi rohan,
use below code :
trigger :
trigger AccountMergeTrigger on Account (after delete) {
       accountTriggerHandler.afterDeleteMethod(trigger.old); 
}
apex class
public class accountTriggerHandler{

  public static void afterDeleteMethod(List<account> triggerOldList){
     List<Account_Merge_History__c> listAccountBackup = new List<Account_Merge_History__c>();
     for(Account acct : triggerOldList){
      if(String.isNotBlank(acct.MasterRecordId)){    
        listAccountBackup.add(new Account_Merge_History__c(Name = acct.Name,ma_a__c =acct.MasterRecordId, ma_b__c = acct.Id, ma_e__c = acct.Id, ma_f__c = acct.MasterRecordId ));
      }
     }
    if(listAccountBackup.size() > 0) {
     insert listAccountBackup;
    }
  }
}
i hope it helps you.
 Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
thanks
sfdcmonkey.com

 

All Answers

N.M. SharmaN.M. Sharma

Hi Rohan,

Please follow the below code. It will help and guide you to create helper class.

trigger AccountMergeTrigger on Account (after delete) {
    AccountHandler.checkNotBlank(Trigger.Old);         
}

create New Class
Public static class AccountHandler{
    public static void checkNotBlank(Account trgOldLst){
       List<Account_Merge_History__c> listAccountBackup = new List<Account_Merge_History__c>();
                for(Account acct : trgOldLst) {
                    if(String.isNotBlank(acct.MasterRecordId)) {    
                        listAccountBackup.add(new Account_Merge_History__c(Name = acct.Name,ma_a__c =acct.MasterRecordId, ma_b__c = acct.Id, ma_e__c = acct.Id, ma_f__c = acct.MasterRecordId ));
                }
            }
        if(listAccountBackup.size() > 0) {
            insert listAccountBackup;
        } 
    }
}

Thanks

sfdcMonkey.comsfdcMonkey.com
Hi rohan,
use below code :
trigger :
trigger AccountMergeTrigger on Account (after delete) {
       accountTriggerHandler.afterDeleteMethod(trigger.old); 
}
apex class
public class accountTriggerHandler{

  public static void afterDeleteMethod(List<account> triggerOldList){
     List<Account_Merge_History__c> listAccountBackup = new List<Account_Merge_History__c>();
     for(Account acct : triggerOldList){
      if(String.isNotBlank(acct.MasterRecordId)){    
        listAccountBackup.add(new Account_Merge_History__c(Name = acct.Name,ma_a__c =acct.MasterRecordId, ma_b__c = acct.Id, ma_e__c = acct.Id, ma_f__c = acct.MasterRecordId ));
      }
     }
    if(listAccountBackup.size() > 0) {
     insert listAccountBackup;
    }
  }
}
i hope it helps you.
 Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
thanks
sfdcmonkey.com

 
This was selected as the best answer
roni shoreroni shore
Perfect Thanks!!