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
SeanCoSeanCo 

How to test for no records returned in 'apex:repeat' statement?

I am trying to figure out how to test fields (included within a apex:repeat) to see if they are blank, or null, and if so display some alternate text (Ex: No records to display) in the table instead of a blank table.  Example code snippet below:

 

 

<apex:repeat var="auditList" value="{!relatedTo.Site_Audit__r}">
   <tr>
    <td>
	<apex:outputField value="{!auditList.Audit_Type__c}" />
    </td>
    <td>
       	<apex:outputField value="{!auditList.Delivery_Date__c}" />
    </td>
    <td>
	<apex:outputField value="{!auditList.Review_Date__c}" />
    </td>
   </tr>
</apex:repeat>

 

Thanks for any help or suggestions! 

 

NaishadhNaishadh

use apex:outputPanel before rendering apex:repeat code.

 

 

<apex:outputPanel id="dataPanel" rendered="{!relatedTo.Site_Audit__r.size() > 0}">
    <!-- your apex repeat code -->
</apex:outputPanel>
<apex:outputPanel id="blankPanel" rendered="{!relatedTo.Site_Audit__r.size() == 0}">
    <!-- error message code -->
</apex:outputPanel>

 

 

SeanCoSeanCo

Naishadh,

 

Thanks for your reply.  I Tried your suggestion (copy of code below), but receive the following the following error when trying to save/deploy:

 

   Result:  FAILED
   Problem: Unknown function relatedTo.Site_Audit__r.size. Check spelling.

Result:  FAILED  

Problem: Unknown function relatedTo.Site_Audit__r.size. Check spelling.

 

Is the syntax incorrect?  Thanks!

 

<apex:outputPanel id="dataPanel" rendered="{!relatedTo.Site_Audit__r.size() > 0}">

	<apex:repeat var="auditList" value="{!relatedTo.Site_Audit__r}">

	   <tr>
		<td>
			<apex:outputField value="{!auditList.Audit_Type__c}" />
		</td>
		<td>
			<apex:outputField value="{!auditList.Delivery_Date__c}" />
		</td>
		<td>
			<apex:outputField value="{!auditList.Review_Date__c}" />
		</td>
	   </tr>
   
   </apex:repeat>

</apex:outputPanel>

<apex:outputPanel id="blankPanel" rendered="{!relatedTo.Site_Audit__r.size() == 0}">

	   <tr>
		<td>
			No records to display.
		</td>
	   </tr>

</apex:outputPanel>

 

 

 

NaishadhNaishadh

Sorry forget to remove brackets :smileyhappy:. 

 

 

<apex:outputPanel id="dataPanel" rendered="{!relatedTo.Site_Audit__r.size > 0}">

 

 

SeanCoSeanCo

Naishadh,

 

I was curious about that myself and actually tried that format after it failed the first time.  It still fails with the brackets removed with the following error:

 

 

   Result:  FAILED
   Problem: Invalid field 'size' for SObject 'Site_Audit__c'.

Result:  FAILED  
Problem: Invalid field 'size' for SObject 'Site_Audit__c'.

 

 

Copy of code:

 

 

   <apex:outputPanel id="dataPanel" rendered="{!relatedTo.Site_Audit__r.size > 0}">
   
    	<apex:repeat var="auditList" value="{!relatedTo.Site_Audit__r}">
   
		   <tr>
		    <td>
				<apex:outputField value="{!auditList.Audit_Type__c}" />
		    </td>
		    <td>
		       	<apex:outputField value="{!auditList.Delivery_Date__c}" />
		    </td>
		    <td>
				<apex:outputField value="{!auditList.Review_Date__c}" />
		    </td>
		   </tr>
	   
	   </apex:repeat>
   
   </apex:outputPanel>  

 

Thanks for all your help!

 

NaishadhNaishadh

I don't know what you have written in your controller class but for your reference see the sample code(Already tested).

VF Page
------
<apex:page controller="TestListSize">
    <apex:outputPanel rendered="{!TestData.size == 0}">
        No Record Exists
    </apex:outputPanel> 
    <apex:outputPanel rendered="{!TestData.size > 0}">
        <apex:repeat value="{!TestData}" var="td">
            <apex:repeat value="{!td.data}" var="dt">
                {!dt}
            </apex:repeat>
        </apex:repeat>
    </apex:outputPanel>    
</apex:page>
------
Apex class
------
public with sharing class TestListSize {

    public TestListSize() {}

    public List<WrapperTest> getTestData() {
        List<String> data = new List<String>();
        data.add('One');
        data.add('two');
        
        List<WrapperTest> dataTest = new List<WrapperTest> ();
        dataTest.add(new WrapperTest(data));
        
        return dataTest;
    }
    
    public class WrapperTest {
        public List<String> data {get; set;}        
        
        public WrapperTest(List<String> dataList) {
            data = dataList;
        }
    }
}

 

 

 

SeanCoSeanCo

Naishadh,

 

I am referencing a Custom Object in this case; it is part of an email template that produces attached PDF.   Full example code is below (irrelevant parts omitted):

 

 

 

<messaging:emailTemplate subject="BLAH" 
recipientType="Contact"
relatedToType="X360_Contract_Cycle__c">

<messaging:plainTextEmailBody >

Email message goes here....

</messaging:plainTextEmailBody>

<messaging:attachment renderAs="pdf" filename="{!relatedTo.name}">

<apex:stylesheet value="{!$Resource.seancss}"/>
.
.
.
<div class="pageBreak">  
 <h4>Site Audit Delivery </h4>
  <table>
   <tr>
    <th>
       Type
    </th>
    <th>
       Date Delivered
    </th>
    <th>
       Date Reviewed
    </th>
   </tr>
   
    	<apex:repeat var="auditList" value="{!relatedTo.Site_Audit__r}">
   
		   <tr>
		    <td>
				<apex:outputField value="{!auditList.Audit_Type__c}" />
		    </td>
		    <td>
		       	<apex:outputField value="{!auditList.Delivery_Date__c}" />
		    </td>
		    <td>
				<apex:outputField value="{!auditList.Review_Date__c}" />
		    </td>
		   </tr>
	   
	   </apex:repeat>
   
  </table>

</div>
.
.
.               
</messaging:attachment>
</messaging:emailTemplate>

 

Given that I am referencing a Custom Object and not a controller class perhaps this method will not work for more?  Maybe I need to try a test using one of the custom operators (Ex: ISBLANK or ISNULL)?  I gave that a shot, but was unsuccessful unfortunately.  Thanks again for all your help on this...truly appreciated!!  

 

jonathanrico.jonathanrico.

I ran into this issue today, here's how I solved it:

 

<apex:variablevar="auxList"value=""/>

 

<!-- Workaround for getting the size of a related list -->

<apex:repeatvalue="{!Custom_Object__c.Related_Object__r}"var="r"rows="1">

<apex:variablevar="auxList"value="{!r.id}"/>

</apex:repeat>

 

<apex:pageMessagesummary="No records "severity="info"strength="2"rendered="{!auxList == ''}"/>

 

<apex:pageMessagesummary="Has records"severity="info"strength="2"rendered="{!auxList != ''}"/>