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
sunithasunitha 

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

Hi All,

 

I am getting the below error  in class while developing visualforcepage for   page reference. Please correct me 

 

 

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

 

Class.MyCustomController.<init>: line 4, column

 

visualforce page code:

<apex:page controller="MyCustomController" tabStyle="Account" >
<apex:form >
<apex:pageBlock title="Accountdetails">
<apex:pageBlockSection title="AccountInformation" collapsible="true">
<apex:inputfield value="{!Account.Name}"/>
<apex:inputfield value="{!Account.Phone}"/>
<apex:inputfield value="{!Account.Industry}"/>
<apex:inputfield value="{!Account.Rating}"/>
<apex:inputfield value="{!Account.Website}"/>
</apex:pageBlockSection>
<apex:pageBlockSection title="Accountdetails" collapsible="true">
<apex:inputfield value="{!Account.BillingCity}"/>
<apex:inputfield value="{!Account.billingCountry}"/>
<apex:inputfield value="{!Account.Shippingcity}"/>
<apex:inputfield value="{!Account.shippingCountry}"/>
<apex:inputfield value="{!Account.Annualrevenue}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 

 

class

 

 

public class MyCustomController {
public Account acc;
public MyCustomController(){
acc=[select name,id,phone,industry,website,rating,BillingCity,BillingCountry,
ShippingCity,ShippingCountry,AnnualRevenue from Account where id=:Apexpages.currentPage().getParameters().get('id')];

}
public Account getAccount() {
return acc;
}
public PageReference saveMethod()
{
update acc;
PageReference pf=new apexPages.StandardController(acc).view();
return pf;
}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
sunithasunitha
Its working thank you for giving the reply -- ~ sunitha ~

All Answers

bob_buzzardbob_buzzard

In this line:

 

acc=[select name,id,phone,industry,website,rating,BillingCity,BillingCountry,
ShippingCity,ShippingCountry,AnnualRevenue from Account where id=:Apexpages.currentPage().getParameters().get('id')];

you are assuming that there will only ever be one row returned.  However, if the id parameter is missing then there will be no matches.

 

Its better to store the query results in a list and then check the size before proceeding:

 

List<Account> accs=[select name,id,phone,industry,website,rating,BillingCity,BillingCountry,
ShippingCity,ShippingCountry,AnnualRevenue from Account where id=:Apexpages.currentPage().getParameters().get('id')];

if (accs.size()>0)
{
  acc=accs[0];
}

 

sunithasunitha
Its working thank you for giving the reply -- ~ sunitha ~
This was selected as the best answer
MunmunMunmun

Hi sunitha, can you please let me know how its working. 

 

Regrds

Sushma