+ Start a Discussion
Dileep KatariDileep Katari 

How to convert this trigger into a trigger with handler class? Can anyone please help?

Trigger UpdateOwnerOfContact on Account(before update) {
 //Map to collect account id with changed owner id(user id ) .
 Map<Id,Id> accIdWithOwnerIdMap = new Map<Id,Id>();
 List<Contact> listOfContactsToUpdate = new List<Contact>();
 for(Account currentAccount : trigger.new) {
     if(currentAccount.OwnerId != Trigger.oldMap.get(currentAccount.Id).OwnerId){
        accIdWithOwnerIdMap.put(currentAccount.Id,currentAccount.OwnerId);
     }         
 }
 if(!accIdWithOwnerIdMap.isEmpty()){
  for(Contact con: [SELECT Id, OwnerId, AccountId FROM Contact 
                    WHERE AccountId IN :accIdWithOwnerIdMap.keySet() ]) {
   con.OwnerId = accIdWithOwnerIdMap.get(con.AccountId);
   listOfContactsToUpdate.add(con);
  }
 }
 if(!listOfContactsToUpdate.isEmpty()) {
  try{
   update listOfContactsToUpdate;
  }catch(DmlException de){
   System.debug(de);
  }
  
 }
}   
Best Answer chosen by Dileep Katari
AmarpreetAmarpreet
Hi Dileep,

Pleas try below Code:

Trigger:
trigger UpdateOwnerOfContact on Account (before insert) {
UpdateOwnerOfContactHandler.UpdateOwner(trigger.new, trigger.oldMap);
}

Handler:
 
public class UpdateOwnerOfContactHandler {
    
    public static void UpdateOwner(List<Account> newTriggeList , Map<id, Account> oldTriggerMap){
        //Map to collect account id with changed owner id(user id ) .
        Map<Id,Id> accIdWithOwnerIdMap = new Map<Id,Id>();
        List<Contact> listOfContactsToUpdate = new List<Contact>();
        for(Account currentAccount : newTriggeList) {
            if(currentAccount.OwnerId != oldTriggerMap.get(currentAccount.Id).OwnerId){
                accIdWithOwnerIdMap.put(currentAccount.Id,currentAccount.OwnerId);
            }         
        }
        if(!accIdWithOwnerIdMap.isEmpty()){
            for(Contact con: [SELECT Id, OwnerId, AccountId FROM Contact 
                              WHERE AccountId IN :accIdWithOwnerIdMap.keySet() ]) {
                                  con.OwnerId = accIdWithOwnerIdMap.get(con.AccountId);
                                  listOfContactsToUpdate.add(con);
                              }
        }
        if(!listOfContactsToUpdate.isEmpty()) {
            try{
                update listOfContactsToUpdate;
            }catch(DmlException de){
                System.debug(de);
            }
            
        }
        
    }
}

 

All Answers

Dileep KatariDileep Katari
I have gone as far as writing down few lines but was stuck when it came down to replacing trigger.oldMap and trigger.new. Any help would be appreciated. Thank you.
AmarpreetAmarpreet
Hi Dileep,

Pleas try below Code:

Trigger:
trigger UpdateOwnerOfContact on Account (before insert) {
UpdateOwnerOfContactHandler.UpdateOwner(trigger.new, trigger.oldMap);
}

Handler:
 
public class UpdateOwnerOfContactHandler {
    
    public static void UpdateOwner(List<Account> newTriggeList , Map<id, Account> oldTriggerMap){
        //Map to collect account id with changed owner id(user id ) .
        Map<Id,Id> accIdWithOwnerIdMap = new Map<Id,Id>();
        List<Contact> listOfContactsToUpdate = new List<Contact>();
        for(Account currentAccount : newTriggeList) {
            if(currentAccount.OwnerId != oldTriggerMap.get(currentAccount.Id).OwnerId){
                accIdWithOwnerIdMap.put(currentAccount.Id,currentAccount.OwnerId);
            }         
        }
        if(!accIdWithOwnerIdMap.isEmpty()){
            for(Contact con: [SELECT Id, OwnerId, AccountId FROM Contact 
                              WHERE AccountId IN :accIdWithOwnerIdMap.keySet() ]) {
                                  con.OwnerId = accIdWithOwnerIdMap.get(con.AccountId);
                                  listOfContactsToUpdate.add(con);
                              }
        }
        if(!listOfContactsToUpdate.isEmpty()) {
            try{
                update listOfContactsToUpdate;
            }catch(DmlException de){
                System.debug(de);
            }
            
        }
        
    }
}

 
This was selected as the best answer