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
Samuel JohnsonCBESamuel 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 );
    }
   
}
Best Answer chosen by Samuel JohnsonCBE
pconpcon
You wouldn't put it on the account page, you would write a new trigger like this
 
trigger reassignContactOwner on Account (after update) {
    Map<Id, List<Contact>> accountToContactMap = new Map<Id, List<Contact>>();
    Set<Id> accountIds = new Set<Id>();
    List<Contact> contactsToUpdate = new List<Contact>();

    for (Account account: Trigger.new) {
        Account oldAccount = Trigger.oldMap.get(account.Id);

        if (account.OwnerId != oldAccount.OwnerId) {
            accountIds.add(account.Id);
        }
    }

    if (!accountIds.isEmpty()) {
        for (Id id: accountIds) {
            accountToContactMap.put(id, new List<Contact>());
        }

        List<Contact> contactsToUpdate = [
            select AccountId,
                OwnerId
            from Contact
            where AccountId in :accountIds
        ];

        for (Contact contact: contactsToUpdate) {
            contact.OwnerId = Trigger.newMap.get(contact.AccountId).OwnerId;
        }

        if (!contactsToUpdate.isEmpty()) {
            update contactsToUpdate;
        }
    }
}

 

All Answers

pconpcon
To do what you'd want, you'd also need a trigger on the Account.  Then get all of the contacts for the updated accounts and then set OwnerId on all of the contacts.
Samuel JohnsonCBESamuel JohnsonCBE
What type of code would I need to put onto the account page
pconpcon
You wouldn't put it on the account page, you would write a new trigger like this
 
trigger reassignContactOwner on Account (after update) {
    Map<Id, List<Contact>> accountToContactMap = new Map<Id, List<Contact>>();
    Set<Id> accountIds = new Set<Id>();
    List<Contact> contactsToUpdate = new List<Contact>();

    for (Account account: Trigger.new) {
        Account oldAccount = Trigger.oldMap.get(account.Id);

        if (account.OwnerId != oldAccount.OwnerId) {
            accountIds.add(account.Id);
        }
    }

    if (!accountIds.isEmpty()) {
        for (Id id: accountIds) {
            accountToContactMap.put(id, new List<Contact>());
        }

        List<Contact> contactsToUpdate = [
            select AccountId,
                OwnerId
            from Contact
            where AccountId in :accountIds
        ];

        for (Contact contact: contactsToUpdate) {
            contact.OwnerId = Trigger.newMap.get(contact.AccountId).OwnerId;
        }

        if (!contactsToUpdate.isEmpty()) {
            update contactsToUpdate;
        }
    }
}

 
This was selected as the best answer
Samuel JohnsonCBESamuel JohnsonCBE
I just tried to use this trigger.  I am getting an   " Error: Compile Error: Duplicate variable: contactsToUpdate at line 19 column 24 "
pconpcon
Sorry, just delete line 04.  This was all from memory and was not tested.
Samuel JohnsonCBESamuel JohnsonCBE
Thank You so much.  That worked very well