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
SFManiSFMani 

List has no rows for assignment to SObject error

I have a custom Object TimeRecords__c and I want to display the VF Page as a pdf in the object using a custom Button.  I created a Visualforce page and a controller class for the Custom Button. On clicking the custom button I get the 'List has no rows to SObject error'. 
My VF Page Code is as below:
<apex:page standardController="TimeRecords__c" showHeader="false" renderas="pdf" extensions="TimeRecord">

<table border="0" cellspacing="0" cellpadding="0" width="100%" id="table1">


<tr>
    <td >
        <img src='{!URLFOR($Resource.VinasLogo)}'  title="logo" />
    </td>
    
    <td  align="right"><font face="Arial" >
    <b>Invoice for {!con.Name}</b>
   </font><br/>
   </td>
  
  
    
</tr>

 
<hr/>

</table>
<table border="0" cellspacing="0" cellpadding="0" width="100%" id="table1">
<tr>    <td><font face="Arial" >
        {!$Organization.Name}<br/>
        {!$Organization.Street}<br/>
        {!$Organization.PostalCode} {!$Organization.City}<br/>
        {!$Organization.Country}<br/>
        </font></td> 
        <td width="60%">&nbsp;</td>
   <!--<td ><font face="Arial">Invoice number: <apex:repeat value="{!Opportunity.Invoices__r}" var="line2">
   {!line2.name}</apex:repeat></font><br/>
  
   
   <font face="Arial">Invoice Date: <apex:repeat value="{!Opportunity.Invoices__r}" var="line2">
   {!line2.Invoice_Date__c}  </apex:repeat></font></td>-->
 
   
  
</tr>
</table>
<br/>
<hr/>
<p><b><font face="Arial" color="#000080">Address Information</font></b></p>

<table border="0" width="100%" id="table2">


<tr>
       <td>          
           <font face="Arial">Bill To:<br/>
                             {!con.MailingStreet}<br/>
                             {!con.MailingCity}{!con.MailingState}{!con.MailingPostalCode}<br/>
                                                          
           </font>
        </td>
        <td width="50%"></td>        
</tr>


</table>
<br/>
<hr/>
<p>&nbsp;</p>
<hr/>
<p align="center"><font face="Arial"><i>Copyright {!$Organization.Name}.</i></font></p>
</apex:page>

Controller Class:
global with sharing class TimeRecord {

    

     public Contact con{get;set;}
    public TimeRecords__c TR{get;set;}
    
    
   
    
    public TimeRecord(ApexPages.StandardController cntrl){
        
     
       this.TR = (TimeRecords__c) cntrl.getRecord();
        TR=[SELECT Billable__c, Activity__c, Client__c, Client_Name__c,Date_of_Work__c, Entry_Type__c, Exclude_From_Invoice__c, Fees_Time__c, Matter__c, Overtime__c, Private_Description__c, Rate__c FROM TimeRecords__c WHERE Name=: ApexPages.currentPage().getParameters().get('id') ];
        
        con=[SELECT Id, Name, MailingStreet, MailingCity, MailingState, MailingPostalCode FROM Contact ];
       
   
    }
    
}

Could someone of you folks help me sort out this error. I have tried many solutions but they don't seem to work.
Thanks in Advance.
Best Answer chosen by SFMani
LBKLBK
I have made a minor change in your controller.

Here is the latest code.

This worked for me.
 
public with sharing class TimeRecord {    

     public List<Contact> con{get;set;}
    public TimeRecord__c TR{get;set;}   
    
   
    public ApexPages.StandardController stdcntrl{get;set;}
    public TimeRecord(ApexPages.StandardController cntrl){
        
     stdcntrl=cntrl;
        
       this.TR = (TimeRecord__c) stdcntrl.getRecord();
        //TR=[SELECT Id, Billable__c, Activity__c, Client__c, Client_Name__c,Date_of_Work__c, //Entry_Type__c, Exclude_From_Invoice__c, Fees_Time__c, Matter__c, Overtime__c, //Private_Description__c, Rate__c FROM TimeRecord__c LIMIT 1];
        TimeRecord__c myTR = [SELECT Id, Client__c FROM TimeRecord__c WHERE Id = :TR.Id];
        con=[SELECT Id, Name, MailingStreet, MailingCity, MailingState, MailingPostalCode FROM Contact WHERE Id=:myTR.Client__c];
       
   
    }
    
}

 

All Answers

LBKLBK
One issue with the APEX class I clearly see is that you are assigning a List<Contact> to Contact type object in the below lines of code.
 
public Contact con{get;set;}


con=[SELECT Id, Name, MailingStreet, MailingCity, MailingState, MailingPostalCode FROM Contact ];

You either have to change your attribute defined, or the SOQL query that populates it.

it could be either
public List<Contact> con{get;set;}
or
 
con=[SELECT Id, Name, MailingStreet, MailingCity, MailingState, MailingPostalCode FROM Contact LIMIT 1];
Apart from this I do not see any issues. And, I don't see the custom button your are talking about, in your VF page.


 
SFManiSFMani
Thanks for your reply. I had tried this earlier too and on updating to:
 
public List<Contact> con{get;set;}

I get the error:
Error: Unknown property 'VisualforceArrayList.Name'

 
SFManiSFMani

OR

updating to:
 

con=[SELECT Id, Name, MailingStreet, MailingCity, MailingState, MailingPostalCode FROM Contact LIMIT 1];

It gives the same error:

'List has no rows to SObject error'
SFManiSFMani
The Custom button is not included in the VF page, but it is created in the TimeRecords__c Object and I select VF page to be displayed on clicking the button.

The VF page is supposed to take data from the TimeRecords__c Object and the related Contact object.
SFManiSFMani
I have made some changes in the code, so I am getting the values but they are always giving results for the first record. What is the error in code? Here is my updated code below:

Controller Class:
public with sharing class TimeRecord {    

     public List<Contact> con{get;set;}
    public TimeRecords__c TR{get;set;}   
    
   
    public ApexPages.StandardController stdcntrl{get;set;}
    public TimeRecord(ApexPages.StandardController cntrl){
        
     stdcntrl=cntrl;
        
       this.TR = (TimeRecords__c) stdcntrl.getRecord();
        TR=[SELECT Id, Billable__c, Activity__c, Client__c, Client_Name__c,Date_of_Work__c, Entry_Type__c, Exclude_From_Invoice__c, Fees_Time__c, Matter__c, Overtime__c, Private_Description__c, Rate__c FROM TimeRecords__c LIMIT 1];
        
        con=[SELECT Id, Name, MailingStreet, MailingCity, MailingState, MailingPostalCode FROM Contact WHERE Id=:TR.Client__c];
       
   
    }
    
}
VF Page:
<apex:page standardController="TimeRecords__c" showHeader="false" renderas="pdf" extensions="TimeRecord">

<table border="0" cellspacing="0" cellpadding="0" width="100%" id="table1">


<tr>
    <td >
        <img src='{!URLFOR($Resource.VinasLogo)}'  title="logo" />
    </td>
    <apex:repeat value="{!con}" var="contact1">
    <td  align="right"><font face="Arial" >
    <b>Invoice for {!contact1.Name}</b>
   </font><br/>
   </td></apex:repeat>
  
  
    
</tr>

 
<hr/>

</table>
<table border="0" cellspacing="0" cellpadding="0" width="100%" id="table1">
<tr>    <td><font face="Arial" >
        {!$Organization.Name}<br/>
        {!$Organization.Street}<br/>
        {!$Organization.PostalCode} {!$Organization.City}<br/>
        {!$Organization.Country}<br/>
        </font></td> 
        <td width="60%">&nbsp;</td>
   <!--<td ><font face="Arial">Invoice number: <apex:repeat value="{!Opportunity.Invoices__r}" var="line2">
   {!line2.name}</apex:repeat></font><br/>
  
   
   <font face="Arial">Invoice Date: <apex:repeat value="{!Opportunity.Invoices__r}" var="line2">
   {!line2.Invoice_Date__c}  </apex:repeat></font></td>-->
 
   
  
</tr>
</table>
<br/>
<hr/>
<p><b><font face="Arial" color="#000080">Address Information</font></b></p>

<table border="0" width="100%" id="table2">
<!--<tr>
       <td colspan="3">
           <font face="Arial">Account name: {!Opportunity.Account.Name} <br/><br/></font>
       </td>
</tr>-->

<tr>
       <td>          
           <font face="Arial">Bill To:<br/>
           <apex:repeat value="{!con}" var="contact1">
                             {!contact1.MailingStreet}<br/>
                             {!contact1.MailingCity}{!contact1.MailingState}{!contact1.MailingPostalCode}<br/>
            </apex:repeat>                                              
           </font>
        </td>
        <td width="50%"></td>        
</tr>


</table>
<br/>
<hr/>
<p>&nbsp;</p>
<hr/>
<p align="center"><font face="Arial"><i>Copyright {!$Organization.Name}.</i></font></p>
</apex:page>


 
LBKLBK
I have made a minor change in your controller.

Here is the latest code.

This worked for me.
 
public with sharing class TimeRecord {    

     public List<Contact> con{get;set;}
    public TimeRecord__c TR{get;set;}   
    
   
    public ApexPages.StandardController stdcntrl{get;set;}
    public TimeRecord(ApexPages.StandardController cntrl){
        
     stdcntrl=cntrl;
        
       this.TR = (TimeRecord__c) stdcntrl.getRecord();
        //TR=[SELECT Id, Billable__c, Activity__c, Client__c, Client_Name__c,Date_of_Work__c, //Entry_Type__c, Exclude_From_Invoice__c, Fees_Time__c, Matter__c, Overtime__c, //Private_Description__c, Rate__c FROM TimeRecord__c LIMIT 1];
        TimeRecord__c myTR = [SELECT Id, Client__c FROM TimeRecord__c WHERE Id = :TR.Id];
        con=[SELECT Id, Name, MailingStreet, MailingCity, MailingState, MailingPostalCode FROM Contact WHERE Id=:myTR.Client__c];
       
   
    }
    
}

 
This was selected as the best answer
SFManiSFMani
on Updating it gives the error:
SObject row was retrieved via SOQL without querying the requested field: TimeRecords__c.Client__c
LBKLBK
Is your object name TimeRecord__c or TimeRecords__c?

I have accidentally mentioned it as TimeRecord__c in my controller code.

Can you change that and try?
SFManiSFMani
I already made that change. Still it gave that error.
LBKLBK
Can you place a debug statement after the lines 12 (to print TR.ID), 14 (to print myTR.Client__c) and 15 (to print con.size())?

Run the VF page once again and check the log file.

Also,  can you please share your controller code as it is, once again?
SFManiSFMani
The error is because in con SOQL query Client__c is referenced as in 
WHERE Id=:TR.Client__c
so I changed to
WHERE Id=:myTR.Client__c
and Added 
public TimeRecords__s myTR {get;set;}
So it finally worked.


 
SFManiSFMani
sorry I just checked you already made this change in your code.My Bad  :)
LBKLBK
You don't have to declare a public property for myTR because it is used only within the Constructor.

But no harm. Good Luck!
SFManiSFMani
myTR is also used in my VF page so it is need to be declared public.