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
SabrentSabrent 

Enterprise Territory Management Assigned User and child Account owner

I have a self relationship on Account object 

Parent Record: Account 1, RecordType = HQ,   Owner =  'Donald  Duck'

Child Record:  Account 2, RecordType = Franchisee,   Owner  =' Billy Wonka'

Child Record:  Account 3,  RecordType = Franchisee,   Owner  =' Billy Wonka'

So, on Account2 I have a field called Parent that looks up to Account 1

In the Territory Hierarchy I have a Territory called 'SOHO'

User assigned to  'SOHO'  =  Billy Wonka

Account assigned to 'SOHO'  = Account 1

When the User assigned to 'SOHO' changes to 'Mickey Mouse'  I want the owner of the Child Records Account 2 and Account 3 to be changed to  'Mickey Mouse'

So basically chnaging the owner on the child records to be the user who is assined to a territory. 

Is that possible ? 

Here is my psedocode:

Trigger on Territory2  after update
If  IsChanged (Territory2. assinedOwner ) 

GET Id of the  Territory's Assignd Owner 

Find Child Records related to the Id

Add the Child Records in a List

For EachItem in the ChildRecordList  get the ownerid 

childrecordOwnerid = Territory's Assignd OwnerID

 

Is this possible in a trigger or a flow ? 

  

 

 

 

 

 

PriyaPriya (Salesforce Developers) 
Hi 

Yes you can achiece this with trigger (after update).

Refer this below sample and modify your code as per you need :- 
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;
  } 
}

Kindly mark it as the best answer if it helps you.

Thank you