4

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

3
  • 1
    Is this a one-time thing, or a process that needs to be enforced going forward (Contact + Account Owners can never be different)?
    – James
    Sep 22, 2015 at 12:54
  • I have already put a trigger in place to make sure everything stays aligned going forward, its the initial clear up I am struggling with
    – user24879
    Sep 22, 2015 at 12:55
  • Okay, great! I'll post answer below!
    – James
    Sep 22, 2015 at 12:56

1 Answer 1

1

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; 
4
  • You cannot update more than 10,000 in a single go. You will have to add a limit to your query (9999 or 10000). In an organisation having 2000 accounts, I believe the number of contact will be large. This will become more of a manual activity. A trigger will be fine and It will require a deployment but it will take care of future cases as well. After deploying a trigger you can perform a blank update on contacts
    – manjit5190
    Sep 22, 2015 at 13:19
  • @manjit_singh, this is meant to be a quick fix as the trigger is already in place. I would wager not every contact has a different owner, and so it would likely fall below the governor limts. Further, the code supplied only counts as DML row if the owner is different (note the IF block). It's a very specific use case with a precise, quick solution. Either way, DataLoader is the best option.
    – James
    Sep 22, 2015 at 13:30
  • Yes, but I read that OP already has a trigger in place, I think he can just run a blank update via data-loader by exporting only ContactIds and then just uploading/updating them and everything will be in place.
    – manjit5190
    Sep 22, 2015 at 13:35
  • 1
    Awesome. That's why I made it option 1 in my recommend :)
    – James
    Sep 22, 2015 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.