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
jaanvivekjaanvivek 

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

Best Answer chosen by Admin (Salesforce Developers) 
Chamil MadusankaChamil Madusanka

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.

 

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
        }   
        
    }

}

 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

MJR_NexMJR_Nex

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

Chamil MadusankaChamil Madusanka

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.

 

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
        }   
        
    }

}

 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.

This was selected as the best answer