function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Uttpal chandraUttpal chandra 

How to change child record owner if i change the owner of parent record?

Hi all,
In account object if i change the owner of parent object then all the child record account get changed upto 3 level.

For eg.
ABC(parent)---->XYZ(child)-->MNO(child of XYZ).

If i change owner of ABC then the owner of XYZ and MNO also got changed


Thanks in advance
Priyanka DPriyanka D
Hi Uttpal,

If they are in master-detail relationship, then you don't need to do anything otherwise you will have to write trigger for that.
Deepali KulshresthaDeepali Kulshrestha
Hi Uttpal,

I Have read your problem. You can do this with the help of trigger. 
trigger===>>

trigger ChangeOwnerOfAccount on Account (After update) {
if(trigger.isAfter && Trigger.isUpdate){
    ChangeOwnerOfAccount.ChangeOwnerwithChildAccount(Trigger.new);
}
}


Handler ====>>

public class ChangeOwnerOfAccount {
public static void ChangeOwnerwithChildAccount(List<Account> aclist){
    List<Account>  childlist = [Select id,Name,ParentId from Account where parentID in : aclist];
    List<Account>  grandchildlist = [Select id,Name,ParentId from Account where parentID in : childlist];
    Map<ID,List<Account>> childMap = new Map<ID,List<Account>>();
    if(childlist != null){
        for(Account ac : childlist){
            if(childMap.containsKey(ac.ParentID)){
                List<Account>  alist = childMap.get(ac.ParentId);
                alist.add(ac);
                childMap.put(ac.ParentId,alist);
            }
            else{
                childMap.put(ac.ParentId , new List<Account>());
                List<Account>  alist = childMap.get(ac.ParentId);
                alist.add(ac);
                childMap.put(ac.ParentId,alist);
            }
        }
    }
    Map<ID,List<Account>> grandChildMap = new Map<ID,List<Account>>();
    if(grandchildlist != null){
        for(Account ac : grandchildlist){
            if(grandChildMap.containsKey(ac.ParentID)){
                List<Account>  alist = grandChildMap.get(ac.ParentId);
                alist.add(ac);
                grandChildMap.put(ac.ParentId,alist);
            }
            else{
                grandChildMap.put(ac.ParentId , new List<Account>());
                List<Account>  alist = grandChildMap.get(ac.ParentId);
                alist.add(ac);
                grandChildMap.put(ac.ParentId,alist);
            }
        }
    }
    List<Account>  updtChild = new List<Account>();
    Integer  x = 0;
    for(ID i : childMap.KeySet()){
        if(aclist[x].Id == i){
            List<Account> alist = childMap.get(i);
            for(Account a : alist){
                a.OwnerId  = aclist[x].OwnerId;
                updtChild.add(a);
            }
        }
        x++;
    }
    update updtChild;
    List<Account>  updtGrandChild = new List<Account>();
    Integer  y = 0;
    for(ID i : grandChildMap.KeySet()){
        if(aclist[y].Id == i){
            List<Account> alist = grandChildMap.get(i);
            for(Account a : alist){
                a.OwnerId  = aclist[y].OwnerId;
                updtGrandChild.add(a);
            }
        }
        y++;
    }
    update updtGrandChild;
  } 
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com