[SalesForce] Change Opportunity Owner through Developer Console

We are not going to renew a Partner portal license. So Every Opportunity ( closed won and others) which is on this license user name has to be transferred to Admin. I tried to do it from developer console (simple coding). Debug shows exactly what I want to happen. I do not know why owner is still the previous one. Yes I have child object on Opportunity and they are also getting executed but still no trigger or class changes the owner.
Here is the code I tried

   list<Opportunity> tempOpp = new list<Opportunity>();
   list<Opportunity> tempSamOpp = [SELECT id, ownerId,user__c,stageName,Partner_Portal__c,BDE_Description__c
                                    from Opportunity
                                    where ownerId = '00590000000nv63'
                    limit 10];

  system.debug('--list size--'+ tempSamOpp.size()+'--list contain--'+tempSamOpp);

  id uAdmin = [SELECT id, name from User where Name = 'Balamurugan Nadar'].id;
  system.debug('--User Admin ID-' + uAdmin);
  id uSam = [SELECT id, name from User where Name = 'Sam T'].id;
  system.debug('--User Sam ID-' + uSam);

  for(Opportunity tempAdminOpp : tempSamOpp){
      system.debug('--OwnerId- '+ tempAdminOpp.OwnerId);
      if(tempAdminOpp.ownerID == uSam){
         system.debug('-is it coming here-');
         tempAdminOpp.ownerID = uAdmin;
         tempAdminOpp.User__c = uAdmin;
        tempAdminOpp.Partner_Portal__c = null;
       //tempAdminOpp.BDE_Description__c = 'It was Sam\' Opportunity';
     }
     system.debug('-OwnerId Admin-' + tempAdminOpp.ownerId +'-User Admin-' +  tempAdminOpp.User__c);
     tempOpp.add(tempAdminOpp);
  }
  system.debug('--tempOpp size--'+ tempOpp.size()+'--tempOpp contains-'+ tempOpp);  
  update tempOpp;

I tried using system.assert(false); to stop execution after update , still other code gets execute. There are more than 700 records of this user.

Best Answer

try

Opportunity[] opportunities = [SELECT /** required Fields **/ 
                               FROM Opportunty 
                               WHERE Owner.Name = /** owner Name **/
                               LIMIT 1];
for(Opportunity opp : opportunities ){
    opp.OwnerId = /** here wanted Id **/
}

update opportunities;

This have to work. If it is still remain old owner then the issue is not your action from developer console but probably some triggered logic.

Related Topic