You need to sign in to do that
Don't have an account?
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...
If(c.owner.Id ==oldOwnerid)
Soo changed that ...It works fine now...
All Answers
Change the line:
to:
If(c.owner.Id ==oldOwnerid)
Soo changed that ...It works fine now...
Changed to?
Hoping to nab this for my deployment!