trigger UpdateContactOwner on Account (before insert, after insert, before update, after update) { set setAccountIds = new set() ; if(Trigger.isAfter && Trigger.isUpdate){ for(Account oAccount : Trigger.New){ if(oAccount.OwnerId != Trigger.oldMap.get(oAccount.Id).OwnerId){ setAccountIds.add(oAccount.Id) ; } } if(!setAccountIds.isEmpty()){ list listAccount = [Select Id, OwnerId, (Select Id, OwnerId from Contacts) from Account Where Id IN: setAccountIds] ; if(listAccount != null && !listAccount.isEmpty()){ list listContact = new list() ; for(Account acc : listAccount){ for(Contact con : acc.Contacts){ con.OwnerId = acc.OwnerId ; listContact.add(con) ; } } if(!listContact.isEmpty()) update listContact ; } } } }