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
Jennifer LaingJennifer Laing 

Trigger to update Child Account Owners to Parent Account Owners when Child created

I have never created a Trigger before but I believe when I'm trying to achieve is (relatively!) simple?!  I would like new Child Account Record Owners to have the same owner as the Parent Account Record Owner when the Child is created.  Is this possible?
Best Answer chosen by Jennifer Laing
HARSHIL U PARIKHHARSHIL U PARIKH
Hi Jennifer,

Here is a trigger I can offer which would satisfy your requirements.

Below trigger would work whenever accounts are inserted with parent account provided, it will automatically take parent account's ownerId into child account's ownerId.
Same goes for an update and Un-delete operation as well.

Trigger Code:
Trigger ParentChildOwner on Account (Before Insert, Before Update, After UnDelete) {
	
    List<Account> comingAccounts		 = New List<Account>();
    List<Id> comingAccountsParentIds	 = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Account act : Trigger.New)
        {
            If(act.ParentId != NULL)
            {
               comingAccounts.add(act);
               comingAccountsParentIds.add(act.ParentId);
            }
            
        }
    }
    
    List<Account> fetchingParentAccount = [Select Id, OwnerID FROM Account WHERE Id =:comingAccountsParentIds AND OwnerID != NULL];
    //system.debug('The size of fetchingParentAccount is: ' + fetchingParentAccount.size());
    
    For(Account EveryAct : comingAccounts)
    {
        //system.debug('EveryAct existing owner id is: ' + EveryAct.OwnerId);
        
        For(Account EveryParent : fetchingParentAccount)
        {
            If(EveryAct.ParentId == EveryParent.Id)
            {
                EveryAct.OwnerId = EveryParent.OwnerId;
            }
        }
        
        //system.debug('EveryAct new owner id is: ' + EveryAct.OwnerId);
    } 
}
Hope this helps and if it resolves the issue then kindly mark it as Best Answer!

 

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH
Hi Jennifer,

Here is a trigger I can offer which would satisfy your requirements.

Below trigger would work whenever accounts are inserted with parent account provided, it will automatically take parent account's ownerId into child account's ownerId.
Same goes for an update and Un-delete operation as well.

Trigger Code:
Trigger ParentChildOwner on Account (Before Insert, Before Update, After UnDelete) {
	
    List<Account> comingAccounts		 = New List<Account>();
    List<Id> comingAccountsParentIds	 = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete){
        For(Account act : Trigger.New)
        {
            If(act.ParentId != NULL)
            {
               comingAccounts.add(act);
               comingAccountsParentIds.add(act.ParentId);
            }
            
        }
    }
    
    List<Account> fetchingParentAccount = [Select Id, OwnerID FROM Account WHERE Id =:comingAccountsParentIds AND OwnerID != NULL];
    //system.debug('The size of fetchingParentAccount is: ' + fetchingParentAccount.size());
    
    For(Account EveryAct : comingAccounts)
    {
        //system.debug('EveryAct existing owner id is: ' + EveryAct.OwnerId);
        
        For(Account EveryParent : fetchingParentAccount)
        {
            If(EveryAct.ParentId == EveryParent.Id)
            {
                EveryAct.OwnerId = EveryParent.OwnerId;
            }
        }
        
        //system.debug('EveryAct new owner id is: ' + EveryAct.OwnerId);
    } 
}
Hope this helps and if it resolves the issue then kindly mark it as Best Answer!

 
This was selected as the best answer
Jennifer LaingJennifer Laing
Thank you so much!  It works perfectly!
Jennifer LaingJennifer Laing
Hello. Would it be possible for you to tell me how to move this from Sandbox to Production please?  I know I need to use Change Sets which is fine, but I’m unsure what to include as I think I need to create an Apex Class as well as the Trigger?
HARSHIL U PARIKHHARSHIL U PARIKH
Hi Jennifer, In order for you to move this trigger in production, you need following items in your change sets.

1) Trigger itself
2) Test classes which gives code covergae to at least 75% of the trigger code.

Since this question is marked sovles, would you be able to create another question asking specific for test classes? Thank you.
Past the link here and I will try to follow up.
Jennifer LaingJennifer Laing
Thank you so much Harshil!
MSongMSong
Harshil - I know it's been a while but any chance you can share test class for this?