+ Start a Discussion
chanti kchanti k 

i am getting below error how to wee can avoid this one

Hi All,

I am getting below error . pls help me on this 
System.QueryException: List has no rows for assignment to SObject 
Class.mySecondController.getAccount: line 5, column 1
public class mydbController {

  public Account getAccount() {
  

    return [select id, name,

            (select id, firstname, lastname

             from Contacts limit 5)

            from Account where id =

             :System.currentPageReference()

             .getParameters().get('id')];

  }

}

 
RD@SFRD@SF
Hi Chanti,

Are you sure the list has elements in it?

I suggest you check for the query result, and return null if the list is empty, and the list if the list is not null

Hope it helps
RD
Lokesh KumarLokesh Kumar
HI Chanti,

As you mentioned System.currentPageReference().getParameters().get('id'); this line meaning is you have to pass any account id in the browser url where you are facing an issue like below.

To get the ID open any account and copy the account ID form the browser URL and paste accordingly like in the below image your issue will get resolved.

User-added image

Thanks !
let me know if you have any question.

 
Tarun J.Tarun J.
Hello Chanti,

You can use below code:
public class mydbController {

  List<Account> accList = new List<Account>();
  public Account getAccount() {
  
	accList = [SELECT id, name,(SELECT id, firstname, lastname FROM Contacts limit 5) FROM Account where id =:System.currentPageReference().getParameters().get('id')];
	
	if(accList.size() > 0){
		return accList;
	}
	else{
		return null;
	}	
  }
}

-Thanks,
TK

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Sree SalesforceSree Salesforce
HI Chanti.

If the query retun no records you will get that error.
So you should add  list.size(). Even If query returns empty records. You wont get that error again. If you add list.size().
public class mydbController {
    list<Account> accList=new list<account>();
  public list<Account> getAccount() {
    accList=[select id, name,
 
            (select id, firstname, lastname
             from Contacts limit 5)
            from Account where id =
             :System.currentPageReference()
             .getParameters().get('id')];
   if(accList.size()>0)
   {
    return accList;
       
   }
      else 
      {
          return null;
      }

  }}