You need to sign in to do that
Don't have an account?
Paula 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;
}
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;
}
Below code should help you.
Let me know if this resolves your problem
All Answers
Below code should help you.
Let me know if this resolves your problem
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?
That might be the reason not all contacts are getting updated.
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;
}
}