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
Jan Kopejtko 2Jan Kopejtko 2 

Mass changing ownership of records to a related record's owner

Hey guys,

I imported a bunch of records to an object.

Now the record owner is set to 'Data Import' - a custom user for data import so I know which are imported and which are not.

Each record has a lookup to Account. I want to change the record ownership of the imported records to the owner of the related Account.

This sounds quite simple, just execute anonymous apex via console, before that I should make a backup and so on.

But I wonder what is the cleanest apex code for such update of record ownership? How would you do it?

Thanks
Jan
Max H. GoldfarbMax H. Goldfarb
  1. Iterate through each Account record you need to update
  2. Find the related Account record owner id for each Account record
  3. Store in map, key should be the Account and the value should be the related Account record owner Id
  4. Iterate a second time throuugh your original Account list
  5. For each Account, get the value from our Map (the value is the related Account Owner Id)
  6. Set the Account Owner Id to the map value
  7. Add Account to new List
  8. After you iterate through each Account in the original list, update the list containing our Accounts with new Owner Id values if that list is not empty

If it is a lot of records, you will need to use a batch class, but I think this is proibably easier to do with DemandTools or DataLoader
vinita chaturvedivinita chaturvedi
DO DATA LOADER
david lope 5david lope 5
Nice information and thanks for sharing this to us. Use our free Omegle alternative to video chat with strangers instantly and meet cool new people. Random video chat available on all mobile devices. https://yipcreations.com/omegle-alternatives/