You need to sign in to do that
Don't have an account?
jaanvivek
VisulaForceError-System.QueryException: List has no rows for assignment to SObject
Hello All, I am trying to create a VFPage for Accounts and show it's related Contacts. i am getting the following error-
System.QueryException: List has no rows for assignment to SObject Class.practiseOneController.getAccount: line 5, column 1
Please find below VF 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')]; } }
please help me in this and pls suggest how can we avoid this type of errors while creating pages and classes
Thanks for your help.
Thanks,
JaanVivek
Use following approach. And also check your parameter and check your query whether that id has a record or not. Following approach will handle the exception.
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.
All Answers
This error usually occurs when you are not passing an id in the URL.
Example:
This works:
Http://NA1.Salesforce.com/apex/mypage?id=XXXXXXXXXXXXXX where XXX is a Salesforce id
This doesn't work (no Salesforce id to pass to your controller)
http://NA1.salesforce.com/apex/mypage
Use following approach. And also check your parameter and check your query whether that id has a record or not. Following approach will handle the exception.
If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.