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

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;
}
}
All Answers
In this line:
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:
Hi sunitha, can you please let me know how its working.
Regrds
Sushma