[SalesForce] How to align existing contact owners with account owners

I have a large instance that has around 2000 accounts, there are also a huge number of contacts.

I have been asked to run something that will reassign all the existing contact owners to match with the account owners.

Is there a best practice way to do this? I have considered a apex class or trigger but it is beyond me to figure out how to start or what direction o should be looking in.

Any help would be greatly appreciated

Regards

Best Answer

You have several options:

  1. Use the salesforce.com DataLoader (simplest)
  2. Run an Execute Anonymous script to do the cleanup (2000 records is not that large)

To use DataLoader, you would need to query the Contact object with the following SOQL query:

select Id, AccountId, OwnerId, Account.OwnerId from Contact

Once the data is exported to .CSV file, you should be able to open it and compare the Account.OwnerId + OwnerId fields. You would then delete every column except for Account.OwnerId (rename to OwnerId)and Id.

Finally, save this file nd run it as an update on the Contact object.

This option will avoid any governor limits, allows you to backup your data, and is the most straightforward/common approach.*

Option 2:

If you want to avoid files, you can run this code snippet in the Execute Anonymous window of the developer console in salesforce.com (including production orgs).

Execute Anonymous Apex:

List<Contact> contactsToChangeOwner = new List<Contact>();


//loop through child contacts and replace owner if different...

for(Contact c :  [select Id, AccountId, OwnerId, Account.OwnerId from Contact]){

    if(c.OwnerId != c.Account.OwnerId){

        //replace current contact Owner...
        c.OwnerId = c.Account.OwnerId;

        contactsToChangeOwner.add(c);

    }


}

update contactsToChangeOwner; 
Related Topic