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
Rodolfo Calvo 3Rodolfo Calvo 3 

Update one account owner

Hello team I have the following: 
public void mergeOwners()
        { 
            String a1;
            String a2;
            
            a1 = sGetOwnerOne;
            a2 = sGetOwnerTwo;
            
            Account src = [SELECT Owner.Id FROM Account where Name like :a1];
            Account dest = [SELECT Id, Owner.Id FROM Account where Name like :a2];
            dest.OwnerId = src.OwnerId;
            update dest;
           
        }

These variables are taken from a two different selectLists
 
public static String sGetOwnerOne{set;get;}
    	public static String sGetOwnerTwo{set;get;}

But at clicking the button in visual force the system shows me the following error: 
List has no rows for assignment to SObject
Does somebody know why this happens?
Thanks in advance. 
 
Best Answer chosen by Rodolfo Calvo 3
Neetu_BansalNeetu_Bansal
Hi Rodolfo,

May be any of list is not returning any record, that's why it is throwing exception. Use the below code instead of the above two queries:
List<Account> src = [SELECT Owner.Id FROM Account where Name like :a1];
List<Account> dest = [SELECT Id, Owner.Id FROM Account where Name like :a2];
For using these records, first check the size and then use the first index like below:
Account acc = new Account();
if( src.size() > 0 )
   acc = src[0];
Let me know, if you need any other information.

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi Rodolfo,

May be any of list is not returning any record, that's why it is throwing exception. Use the below code instead of the above two queries:
List<Account> src = [SELECT Owner.Id FROM Account where Name like :a1];
List<Account> dest = [SELECT Id, Owner.Id FROM Account where Name like :a2];
For using these records, first check the size and then use the first index like below:
Account acc = new Account();
if( src.size() > 0 )
   acc = src[0];
Let me know, if you need any other information.

Thanks,
Neetu
This was selected as the best answer
Rodolfo Calvo 3Rodolfo Calvo 3
Thanks a lot! :D