You need to sign in to do that
Don't have an account?
Samuel JohnsonCBE
changing contact owner when account owner changes
I have this trigger, this works when Im creating a new contact under the accounts. What I was looking for is a trigger that does what this does when a new contact is created, but also updates the contact owner when the account owner changes manually. Any thoughts of how to change the code.
trigger reassignContactOwnerToAccountOwner on Contact ( before insert, before update ) {
List<Id> accountIds = new List<Id>();
Map<Id, Id> accountOwnerIdMap = new Map<Id, Id>();
// all the accounts whose owner ids to look up
for ( Contact c : Trigger.new ) {
accountIds.add( c.accountId );
}
// look up each account owner id
for ( Account acct : [ SELECT id, ownerId FROM account WHERE id IN :accountIds ] ) {
accountOwnerIdMap.put( acct.id, acct.ownerId );
}
// change contact owner to its account owner
for ( Contact c : Trigger.new ) {
c.ownerId = accountOwnerIdMap.get( c.accountId );
}
}
trigger reassignContactOwnerToAccountOwner on Contact ( before insert, before update ) {
List<Id> accountIds = new List<Id>();
Map<Id, Id> accountOwnerIdMap = new Map<Id, Id>();
// all the accounts whose owner ids to look up
for ( Contact c : Trigger.new ) {
accountIds.add( c.accountId );
}
// look up each account owner id
for ( Account acct : [ SELECT id, ownerId FROM account WHERE id IN :accountIds ] ) {
accountOwnerIdMap.put( acct.id, acct.ownerId );
}
// change contact owner to its account owner
for ( Contact c : Trigger.new ) {
c.ownerId = accountOwnerIdMap.get( c.accountId );
}
}
All Answers