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
Kevin LanguedocKevin Languedoc 

Change Opportunity Ownership using Apex or DML

Hi

I need to change the ownership of several hundred Opportunities. The following appears to work (no errors) however when I run a query, the test opportunity assigned to this person is still assigned to the original owner. It's as is the record is not getting updated. I have tried the Mass Reassign Opportunities with the same result. The records remain with original owner.

Where is the problem. Btw I am system administrator

List<Opportunity> ops= new List<Opportunity>([select Owner.Name from Opportunity where Owner.Id='005m0000000bLBhAAM']);


if (!ops.isEmpty()){
for(Opportunity o: ops){
     o.Owner.Id='005600000017YNqAAM';
        o.Owner.FirstName = 'Kevin';
        o.Owner.LastName = 'Languedoc';
      System.debug(ops); 
}
update ops;
}
Best Answer chosen by Kevin Languedoc
Alex TennantAlex Tennant
Rather than updating the owner field directly you just need to update the OwnerId field on the Opportunity, change your query and loop to the following:

List<Opportunity> ops= [SELECT OwnerId FROM Opportunity WHERE OwnerId='005m0000000bLBhAAM'];

for(Opportunity o: ops) {
   o.OwnerId='005600000017YNqAAM';
}

System.debug(ops);
update ops;



A couple of other tips:
  • You don't need new List<Opportunity>(...) when assigning the result of a query directly to a list, the platform will cover this for you.
  • You don't need to check if the List is empty before you loop, if the list is empty then the loop will simply do no iterations. In addition to this if you provide an empty List to an update then the update will be skipped.

All Answers

Shyam BhundiaShyam Bhundia
Hi Sandra,

Try:
for(Opportunity o: ops){
     o.OwnerId='005600000017YNqAAM';
      System.debug(ops);
}

You don't need to reference the ID of the owner, you should use OwerId field on the op.  You also dont need to set the owner name, as this will be picked up when the owner is changed,
Alex TennantAlex Tennant
Rather than updating the owner field directly you just need to update the OwnerId field on the Opportunity, change your query and loop to the following:

List<Opportunity> ops= [SELECT OwnerId FROM Opportunity WHERE OwnerId='005m0000000bLBhAAM'];

for(Opportunity o: ops) {
   o.OwnerId='005600000017YNqAAM';
}

System.debug(ops);
update ops;



A couple of other tips:
  • You don't need new List<Opportunity>(...) when assigning the result of a query directly to a list, the platform will cover this for you.
  • You don't need to check if the List is empty before you loop, if the list is empty then the loop will simply do no iterations. In addition to this if you provide an empty List to an update then the update will be skipped.

This was selected as the best answer
Kevin LanguedocKevin Languedoc
Thanks Shyam and Alex

Both you answers are correct and have worked, at least in the Sandbox. I will try it out in prod.

Thanks