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
Ashwini Patil 32Ashwini Patil 32 

DML

Hello,
I am trying to update account owner on the account object using DML. Below is my basic code, when executed, shows the Log as Successful. When I check on the record in Salesforce it shows the old owner, not the updated owner. I am not sure what's going wrong. Any help would be apprecitated. 
User u = [SELECT Id, Name, IsActive FROM User WHERE username = 'test@test.com];
List<Account> myList = [SELECT  Owner.id, Owner.Name FROM Account where Owner.Name = 'ABC'];
    for(Account acc: myList){
    acc.Owner.Id = u.Id;    
    }
Update myList;

Thanks!
NagendraNagendra (Salesforce Developers) 
Hi Ashwini,

Please try the below code and let me know if it works for you.
trigger AccountOwnerChange on Account (before update) {
     Id adminId = [Select Id from User where UserName = 'yourAdminusername'].Id;
     for(Account acc : Trigger.New) {
              acc.OwnerId = UserInfo.getUserId();
           }
      }
Hope this helps.

Thanks,
Nagendra
 
Steven NsubugaSteven Nsubuga
Try this
User u = [SELECT Id, Name, IsActive FROM User WHERE username = 'test@test.com];
List<Account> myList = [SELECT  OwnerId, Owner.Name FROM Account where Owner.Name = 'ABC'];
    for(Account acc: myList){
        acc.OwnerId = u.Id;    
    }
Update myList;

 
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Ashwini

Please try the below code:
User u = [SELECT Id, Name, IsActive FROM User WHERE username = 'test@test.com];
List<Account> myList = [SELECT  Owner.id, Owner.Name FROM Account where Owner.Name = 'ABC'];
    for(Account acc: myList){
    acc.OwnerId = u.Id;    
    }
Update myList;


The only change is : Instead of 'owner.Id' we need to use 'ownerId'.

Hope this will be helpful.

Thanks
Bhargavi.