You need to sign in to do that
Don't have an account?
Deek
Account Owner to Contact Owner trigger
Hi All,
Below is the trigger to change Contact Owner when the Account Owner changes, applicable only to 2 Account record types and for others it shouldnt change the Contact Owner.
For some reason its changing the contact owner for all account record types.
I am not able to figure out where I have committed this silly mistake if so.
Please help to resolve this.
Thanks in advance.
trigger reassignContactOwner 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>();
List<Contact> contactsToUpdate = 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);
}
}
List<Account> accountsWithContacts = [SELECT Id, (SELECT Id, OwnerId FROM Contacts) FROM Account WHERE
(RecordTypeId ='012200000004bhnAAA' OR RecordTypeId ='012200000004bhsAAA') // change for these two record types only.
AND Id in :accountIds];
for(Account a: accountsWithContacts){
for(Contact c: a.Contacts){
String newOwnerId = newOwnerIds.get(a.Id);
String oldOwnerId = oldOwnerIds.get(a.Id);
if (c.OwnerId != oldOwnerId)
{
c.OwnerId=newOwnerId;
contactsToUpdate.add(c);
}
}
}
if (!contactsToUpdate.IsEmpty())
update contactsToUpdate;
}
Below is the trigger to change Contact Owner when the Account Owner changes, applicable only to 2 Account record types and for others it shouldnt change the Contact Owner.
For some reason its changing the contact owner for all account record types.
I am not able to figure out where I have committed this silly mistake if so.
Please help to resolve this.
Thanks in advance.
trigger reassignContactOwner 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>();
List<Contact> contactsToUpdate = 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);
}
}
List<Account> accountsWithContacts = [SELECT Id, (SELECT Id, OwnerId FROM Contacts) FROM Account WHERE
(RecordTypeId ='012200000004bhnAAA' OR RecordTypeId ='012200000004bhsAAA') // change for these two record types only.
AND Id in :accountIds];
for(Account a: accountsWithContacts){
for(Contact c: a.Contacts){
String newOwnerId = newOwnerIds.get(a.Id);
String oldOwnerId = oldOwnerIds.get(a.Id);
if (c.OwnerId != oldOwnerId)
{
c.OwnerId=newOwnerId;
contactsToUpdate.add(c);
}
}
}
if (!contactsToUpdate.IsEmpty())
update contactsToUpdate;
}
I hope below link help you!
https://developer.salesforce.com/forums/ForumsMain?id=906F000000093eFIAQ
Thanks,
Rockzz
I checked this link but not able to modify my code as per my reqiuirements where I need to change only for 2 record types and not others.
Could you please revisit my code and let me know where I am committed the mistake.
Trying using system.debug() to see the list on debug log. Or another option is not to query the RecordType and use a if statement inside the loop check the recordtype. If this works then you know the query was not fetching it correctly.