This error usually occurs when you are not passing an id in the URL
Question:
System.QueryException: List has no rows for assignment to SObject Class.practiseOneController.getAccount: line 5, column 1VF Page:
<apex:page controller="practiseOneController" tabStyle="Account"> <apex:pageBlock title="Hello {!$User.FirstName}!!!"> You belong to {!account.name} Account. </apex:pageBlock> <apex:pageBlock title="Contacts"> <apex:dataTable value="{!account.contacts}" var="contacts" cellpadding="4" border="1"> <apex:column value="{!contacts.FirstName}" headerValue="FirstName"/> <apex:column value="{!contacts.LastName}" headerValue="LastName"/> </apex:dataTable> </apex:pageBlock> </apex:page>Apex Class:
public class practiseOneController { public Account getAccount() { return [select id,Name,(select id,firstname,lastname from Contacts limit 5) from Account where id= :System.currentPageReference().getParameters().get ('id')]; } }
Solution:
Use following code for Apex Class public class practiseOneController { public Account getAccount() { Account[] acc = [select id,Name,(select id,firstname,lastname from Contacts limit 5) from Account where id=:System.currentPageReference().getParameters(). get('id')]; if(acc.size() > 0) { return acc[0]; } else { return null } } }Refer:
System.QueryException
No comments:
Post a Comment