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

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
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')]; } }
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
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.
Thanks !
let me know if you have any question.
You can use below code:
-Thanks,
TK
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
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;
}
}}