You need to sign in to do that
Don't have an account?
Dileep 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);
}
}
}
//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);
}
}
}
Pleas try below Code:
Trigger:
Handler:
All Answers
Pleas try below Code:
Trigger:
Handler: