[SalesForce] Search Functionality

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>();
run=false;
}
public PageReference runQuery()
{
run=true;
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';
process_unit(Database.query(queryunit));  
}
private void process_unit(List<Units__c> unitsale)
{
Unt_sale.clear();
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)
{
unit=unt;
}
} 
}

<apex:page controller="sample_search" >
<apex:pageBlock>
<apex:inputField value="{!cont.ParentId}">
 <apex:inputText value="{!ship_partyNo}"  id="accinfo1" />
 <apex:commandButton value="Go" action="{!runQuery}">
 </apex:pageBlock>
 <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}"/>
   </apex:repeat>   
 </apex:pageBlock>
</apex:page>

Best Answer

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:

<apex:pageMessages/>
Related Topic