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
verynewbieverynewbie 

Trigger to update contact owner when account owner changes

Hi..

 

I need a trigger which will work anytime the Account Owner changes, update all Account Contacts to that same Owner as their related Account.

 

So I created a trigger..Which doesn't work..To test it i created a account with two contacts .So i get contact owner same as account owner.i manually changed one contact owner to another user

So my account owner ,contact1 owner are user1 and contact 2 owner is user2

 

and changed the account owner/Out of two,only one contac whose owner id is same as account changes and other doesnt.

This is my trigger ..Please help me where i am going wrong.

 

 

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;
}

 Pease help i need this asap...

 

Best Answer chosen by Admin (Salesforce Developers) 
verynewbieverynewbie
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...

All Answers

k_bentsenk_bentsen

Change the line:

Contact[] contactUpdates = new Contact[0]; 

 

to:

List<Contact> contactUpdates = new List<Contact>(); 
verynewbieverynewbie
Thanks for that but it didnt solve my problem.I still have only one contact chnaged its owner ID ...Another one doesnt change
asish1989asish1989
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>();
      List<Contact> contactUpdates = new List<Contact>(); 
     
      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(Id = c.Id, OwnerId = newOwnerId);
               contactUpdates.add(updatedContact);
               }
            }
            
            }
       }
            update contactUpdates;
}

 

verynewbieverynewbie
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...
This was selected as the best answer
fourfourfunfourfourfun

Changed to?

 

Hoping to nab this for my deployment!