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
Paula Jarvis 4Paula Jarvis 4 

Trigger to Update Contact Owner Only when Account Owner Changes

I have the following Trigger to Update Contact Owner Only when Account Owner Changes however only some of the Contact Owners are being updated. It's not updating any other child records, which is great. I just need to understand why it won't update ALL Contacts on an Account.

trigger Changeownertrigger on Account (after update) {
       
          Set<Id> accountIds = new Set<Id>();
          Map<Id, String> oldOwnerIds = new Map<Id, String>();
          Map<Id, String> newOwnerIds = new Map<Id, String>();
          Contact[] contactUpdates = new Contact[0];
          
          for (Account a : Trigger.new)
          {
             if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId)
             {
                oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId);
                newOwnerIds.put(a.Id, a.OwnerId);
                accountIds.add(a.Id);
             }

          }
            if (!accountIds.isEmpty()) {
             for (Account acc : [SELECT Id, (SELECT Id, OwnerId FROM Contacts) FROM Account WHERE Id in :accountIds])
                {
                String newOwnerId = newOwnerIds.get(acc.Id);
                String oldOwnerId = oldOwnerIds.get(acc.Id);
                for (Contact c : acc.Contacts)
                {
                   if (c.OwnerId == oldOwnerId) 
                   {
                   Contact updatedContact = new Contact(OwnerId = newOwnerId);
                   contactUpdates.add(updatedContact);
                   }
                }
                 
                }
           }
                update contactUpdates;
    }
Best Answer chosen by Paula Jarvis 4
Lalit Mistry 21Lalit Mistry 21
Hi Paula,
Below code should help you.
trigger Changeownertrigger on Account (after update) {
       
          Set<Id> accountIds = new Set<Id>();
          Map<Id, String> oldOwnerIds = new Map<Id, String>();
          Map<Id, String> newOwnerIds = new Map<Id, String>();
          Contact[] contactUpdates = new Contact[0];
          
          for (Account a : Trigger.new)
          {
             if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId)
             {
                oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId);
                newOwnerIds.put(a.Id, a.OwnerId);
                accountIds.add(a.Id);
             }

          }
            if (!accountIds.isEmpty()) {
             for (Account acc : [SELECT Id, (SELECT Id, OwnerId FROM Contacts) FROM Account WHERE Id in :accountIds])
                {
                String newOwnerId = newOwnerIds.get(acc.Id);
                String oldOwnerId = oldOwnerIds.get(acc.Id);
                for (Contact c : acc.Contacts)
                {
                   if (c.OwnerId == oldOwnerId) 
                   {
                   Contact updatedContact = new Contact(OwnerId = newOwnerId, Id = c.Id);
                   contactUpdates.add(updatedContact);
                   }
                }
                 
                }
           }
                update contactUpdates;
    }

Let me know if this resolves your problem

All Answers

Lalit Mistry 21Lalit Mistry 21
Hi Paula,
Below code should help you.
trigger Changeownertrigger on Account (after update) {
       
          Set<Id> accountIds = new Set<Id>();
          Map<Id, String> oldOwnerIds = new Map<Id, String>();
          Map<Id, String> newOwnerIds = new Map<Id, String>();
          Contact[] contactUpdates = new Contact[0];
          
          for (Account a : Trigger.new)
          {
             if (a.OwnerId != Trigger.oldMap.get(a.Id).OwnerId)
             {
                oldOwnerIds.put(a.Id, Trigger.oldMap.get(a.Id).OwnerId);
                newOwnerIds.put(a.Id, a.OwnerId);
                accountIds.add(a.Id);
             }

          }
            if (!accountIds.isEmpty()) {
             for (Account acc : [SELECT Id, (SELECT Id, OwnerId FROM Contacts) FROM Account WHERE Id in :accountIds])
                {
                String newOwnerId = newOwnerIds.get(acc.Id);
                String oldOwnerId = oldOwnerIds.get(acc.Id);
                for (Contact c : acc.Contacts)
                {
                   if (c.OwnerId == oldOwnerId) 
                   {
                   Contact updatedContact = new Contact(OwnerId = newOwnerId, Id = c.Id);
                   contactUpdates.add(updatedContact);
                   }
                }
                 
                }
           }
                update contactUpdates;
    }

Let me know if this resolves your problem
This was selected as the best answer
Paula Jarvis 4Paula Jarvis 4
Hello Lalit, It still will not transfer ALL Contacts at the Account that are owned by a default “Sys Admin” User. We do have Territories enabled so if the User is not in that particular Territory then the Trigger does not work. PERFECT. I’m stumped as to why the Sys Admin Ownership will not change on the Contacts. Any thoughts? Paula J Jarvis Certified Salesforce Administrator E pjarvis@phreesia.com M (815) 355-3082
Paula Jarvis 4Paula Jarvis 4
Hello Lalit,
 
It still will not transfer ALL Contacts at the Account that are owned by a default “Sys Admin” User. We do have Territories enabled so if the new User is not in that particular Territory then the Trigger does not transer the Contact records. PERFECT. However, I’m stumped as to why the Sys Admin Ownership will not change on the Contacts. Any thoughts?
 
Lalit Mistry 21Lalit Mistry 21
You have the contact ownership check on line 25 which updates contact owner only if current contact owner is same as old account owner.
That might be the reason not all contacts are getting updated.
Syed Wasim 13Syed Wasim 13
Hi Community,
Is anything wrong in my code, please let me know.



trigger OwnerUpdate on Account (after update) {
    set<ID> AccID = new set<ID>();
List<Contact> c = new List<Contact>();
for(Account acc : trigger.new)
{
   AccId.add(acc.ID);
}

if(AccId!=null)
{
List<Contact> Conlist = [Select id,ownerid,Account.OwnerID from Contact where AccountID IN : AccId];
{
for(Contact Con : ConList)
{
  con.ownerId = con.Account.OwnerId;
   c.add(con);
}
}
insert c;
}

}