I have a custom object which is having a lookup relationship with Account. I am trying to display the Custom object values in VF Tab as a report based on the search criteria.We have two search criteria, one is for Account lookup it has to search in custom object based on the lookup selected value and other one is a text box which needs to enter the name. The user needs to search either using lookup value or text box value. I have written a code to achieve this I am able retrieve the data based on lookup value but when I try using text box it throws an error

System.QueryException: List has no rows for assignment to SObject Error is in expression '{!runQuery}' in component in page csr_report

Class.CSR_Report.runCSRQuery: line 16, column 1

Can any one please help me out in this. I have attached my VFpage and Apex for your reference.

Public class sample_search
public List<sample_unit>Unt_sale{get;set;}
public string partyNo{get;set;}
public Boolean run{get;set;}
public Id AccountId;
public Account cont {get;set;}
public sample_search()
Unt_sale=new List<sample_unit>();
public PageReference runQuery()
cont=[select id, Name,ParentId  from Account where id=:cont.ParentId limit 1];    
  AccountId = cont.Id;
string queryunit='select id,name,subject__c,detail__c,Account__r.Id  from Units__c where name=:partyNo OR Account__r.ID =:AccountId';
private void process_unit(List<Units__c> unitsale)
for(Units__c cu:unitsale)
Unt_sale.add(new sample_unit(cu));
//wrapper class for sample_unit
public with sharing class sample_unit
public Units__c unit{get;set;}
public sample_unit(Units__c unt)

<apex:page controller="sample_search" >
<apex:inputField value="{!cont.ParentId}">
 <apex:inputText value="{!ship_partyNo}"  id="accinfo1" />
 <apex:commandButton value="Go" action="{!runQuery}">
 <apex:pageBlock rendered="{!run}">
   <apex:repeat value="{!Unt_sale}" var="us">
   <apex:outputField value="{!us.unit.Name}"/>
   <apex:outputField value="{!us.unit.subject__c}"/>
   <apex:outputField value="{!us.unit.detail__c}"/>

2 Answers 2


This is a SOQL QueryException. From the apex developer book:

if nothing is returned, and the attempt to assign the return value to a single object results in a QueryException.

(you can download this book as PDF here)

This is because the the SOQL query returned nothing for the given search parameter. You need to catch this error with try/catch block:

try {
    cont=[select id, Name,ParentId  from Account where id=:cont.ParentId limit 1];   
catch(QueryException qe){
    ApexPages.addMessage(ApexPages.Message(ApexPages.Severity.ERROR, qe.getMessage()));   

To be able to see this error message at the page you need to add the following viasualforce tag to your page:

  • thank you its working fine but there is one issue when i select the value form lookup and click the button it shows the values but the lookup field remains empty.I need to display the value after search in the lookup field.
    – Udaya
    Commented Dec 19, 2013 at 6:09

This error comes when the query returns no rows. To handle this, change the variable "cont" to List of Account from SObject Account. Then the rest of code should be in the following if condition :

public List<Account> cont {get;set;}

......rest of the code


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .