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

Error message - System.QueryException: List has no rows for assignment to SObject

I am new trying to generate a JSON output, however I encounter the below error message. It would be greatly appreciated if you could provide your valuable inputs in resolving this issue.


System.QueryException: List has no rows for assignment to SObject 


Class.myAccCon.: line 5, column 7 External entry point


My class details is given below



public class myAccCon {
// constructor, gather the context and run the query
 public Account acc{get; private set;}
 public myAccCon (){
acc = [
select id,name
from Account
where name =
           limit 1];



Visual Force page details is given below
<apex:page controller="myAccCon" contentType="text/javascript" showHeader="false" >
{'Query Info':'{!$CurrentPage.parameters.accountName}', 
'Account Owner ':' {!}', 
I am calling the below URL,<username>&pw=<password>&startURL=/apex/accountInfo?accountName=Edge+Communications


That exception is thrown because your query is not returning any records.


One thing you can do is rather than declaring the Account single reference create a list or array of it.


Like:   public Account[] acc{get; private set;}


This will fix the problem, but do check if acc is not null and its size is greater than 1 before using it.


Rahul S.ax961Rahul S.ax961



I believe there's nothing returned by the method : ApexPages.currentPage().getParameters().get('context')

or there is no record with such name,

Just use system.debug('========'+ApexPages.currentPage().getParameters().get('context'));

to know the cause.