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 

Changing owner of a record in a for each loop, how?

Hey, I have object A.

A has lookup to Account. All records on A have the same owner.

I need to change the owner of A records to the owner of the Account in lookup.

Example: John owns 10 records of object A. All of those records have lookup to an Account called McDonalds. Owner of the McDonalds record is user McDonald. I need to change ownership of John's records to the user in related Account (that might not only be McDonald, but others...).

I'm experiencing difficulties with my code:

List <TestObj__c> list1 = New List<TestObj__c>();
list1 = [SELECT id, NAME From TestObj__c WHERE (OwnerId = '0052o000009Stq8')];
for(TestObj__c a :list1) {
   
    /**
   /* variable a;
   /* variable a = [Select AccountOwner from Account where (TestObj__c.lookupToAccount equals AccountId];
    /* a.Owner = a;
    /*
                                                          
}

The for look works, but I don't know what to put inside, because it gives me errors, so I just deleted it and put something vague inside and commented it. How do I do this?
Best Answer chosen by Jan Kopejtko 2
Anthony McDougaldAnthony McDougald
Good Evening Jan,
Hope that your day is off to an amazing start. The code below will query all object A records where the related Account record's owner is not the same (this is under the assumption that the record owner API name of object A is OwnerId). It will then loop through the results, change the record owner of object A to the record owner of the related Account record. It will add each record to another list and update all the records at once with DML so you follow best practices and don't hit any governor limits. Please test and report back when you get the chance. Hope this helps and may God bless you abundantly.
List <TestObj__c> list1 = [SELECT Id, Name, Account__r.OwnerId, OwnerId From TestObj__c WHERE OwnerId != Account__r.OwnerId];
List <TestObj__c> list2 = New List<TestObj__c>();
 for(TestObj__c a :list1){
	a.OwnerId = a.Account__r.OwnerId;
	list2.add(a);
}
update list2;

Best Regards,
Anthony McDougald​​​​​​​ 

All Answers

Anthony McDougaldAnthony McDougald
Good Evening Jan,
Hope that your day is off to an amazing start. The code below will query all object A records where the related Account record's owner is not the same (this is under the assumption that the record owner API name of object A is OwnerId). It will then loop through the results, change the record owner of object A to the record owner of the related Account record. It will add each record to another list and update all the records at once with DML so you follow best practices and don't hit any governor limits. Please test and report back when you get the chance. Hope this helps and may God bless you abundantly.
List <TestObj__c> list1 = [SELECT Id, Name, Account__r.OwnerId, OwnerId From TestObj__c WHERE OwnerId != Account__r.OwnerId];
List <TestObj__c> list2 = New List<TestObj__c>();
 for(TestObj__c a :list1){
	a.OwnerId = a.Account__r.OwnerId;
	list2.add(a);
}
update list2;

Best Regards,
Anthony McDougald​​​​​​​ 
This was selected as the best answer
Jan Kopejtko 2Jan Kopejtko 2
@Anthony McDougald

Hey man, it's working perfectly! Thank you so much man!