You need to sign in to do that
Don't have an account?

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;
}
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;
}
A couple of other tips:
All Answers
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,
A couple of other tips:
Both you answers are correct and have worked, at least in the Sandbox. I will try it out in prod.
Thanks