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
fourfourfunfourfourfun 

Set Contact Owner = Account Owner on Account Owner update

I would LOVE to pilfer the knowledge spelt out in this thread below:

https://developer.salesforce.com/forums/ForumsMain?id=906F000000093fwIAA

but the last known state was the OP posting "oh I found a way to fix it" without actually stating what it was. Anyone reckon they can help pick up the loose end for me.

Essentially, this is his code:

trigger Changeownertrigger on Account (after insert,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(act.Id);
            String oldOwnerId = oldOwnerIds.get(act.Id);
            for (Contact c : acc.Contacts)
            {
               if (c.OwnerId == oldOwnerId)
               {
               Contact updatedContact = new Contact(Id = c.Id, OwnerId = newOwnerId);
               contactUpdates.add(updatedContact);
               }
            }
            }
       }
            update contactUpdates;
}
With a final comment of:

I was able to figure out the solution.My trigger was skipping the if condition.

If(c.owner.Id ==oldOwnerid)

Soo changed that ...It works fine now...
Apologies for asking something that is probably simple but I'm yet to plod off on my Apex courses!
khillan bhardwajkhillan bhardwaj
Hi,

i have rewrite the code as follow.Please check and let me know i your problem ger resolved:

trigger Changeownertrigger on Account (after insert,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,OwnerId (SELECT Id, OwnerId FROM Contacts) FROM Account WHERE Id in :accountIds])
            {
            String newOwnerId = newOwnerIds.get(act.Id);
            for (Contact c : acc.Contacts)
            {
               if (c.OwnerId != acc.ownerId) // or you can check it as you did like   if (c.OwnerId != newOwnerId)
               {
               Contact updatedContact = new Contact(Id = c.Id, OwnerId = acc.ownerId); // you can write as you have write it Contact updatedContact = new Contact(Id = c.Id, OwnerId = newOwnerId);
               contactUpdates.add(updatedContact);
               }
            }
            }
       }
            update contactUpdates;
}// if you write it as i write then you do not have any need to create two map 1. newOwnerIds anfd 2. OldOwnerIds
ShashForceShashForce
Hi,

With that line, the user was trying to update the contact owners only if the contact owner is same as the previous account owner. That probably did not work with his/her test records, but not necessarily wrong.

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank