Posts

Showing posts from March 26, 2023

WAT if the owner of an account is changed then the owner of the related contacts should also be updated.

 Account Trigger ============================================ trigger AccountTrigger on Account (after update) {   Set<ID> accountId = new Set<ID>();   if(Trigger.IsAfter && Trigger.IsUpdate){     for(Account objAcc : trigger.new){       if(trigger.oldMap.containsKey(objAcc.id)){         if(trigger.oldMap.get(objAcc.id).OwnerId != objAcc.OwnerId){           accountId.add(objAcc.Id);         }       }     }     if(accountId != null && accountId.size() > 0 ){       AccountTriggerHandler.UpdateOwnerofContact(Trigger.newMap, accountId);     }   } } ============================================ AccountTriggerHandler Class public with sharing class AccountTriggerHandler {   public static void UpdateOwnerofContact(Map<Id,Account>accountMap,Set<id>accountId){     List<Contact> lstUpdateOwnerofContact = new List<Contact>();     Map<id,Contact> mapofIdToContact = new Map<id,Contact>([SELECT Id,Name,AccountId From contact WHERE Acc