Salesforce (VisualForce): how to check for missing records returned in apex: repeat statements?

I am trying to figure out how to test the fields (included in the top: repeat) to see if they are empty or zero, and if so, display some alternative text (for example: no entries) on the empty table. Existing 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> 

So, in the pseudocode, I am looking for a test, for example:

 IF RELATED RECORDS FOUND FOR APEX:REPEAT PERFORM FOLLOWING: <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> ELSE IF NO RELATED RECORDS PERFORM FOLLOWING: <tr> <td> No records to display. </td> </tr> 

Thanks in advance for your help!


Update in response to the first answer from "eyecream"


Give the apex: pageBlock shot method, but when you try to save / expand, I encountered the following error:

Result: FAILED Problem: <messaging:emailTemplate> cannot contain <apex:pageBlock>.

Now this is an email template that creates an attached PDF (see the general code diagram below). So what is the case ... pageBlock is not allowed in the email template? Thanks for the help!

 <messaging:emailTemplate subject="Your requested quote #{!relatedTo.Name}" recipientType="Contact" relatedToType="X360_Contract_Cycle__c"> <messaging:plainTextEmailBody > . . . </messaging:plainTextEmailBody> <messaging:attachment renderAs="pdf" filename="{!relatedTo.name}"> . . . <apex:pageBlock rendered="{!AND(NOT(ISNULL(auditList)),auditList.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:pageBlock> <apex:pageBlock rendered="{!OR(ISNULL(auditList),auditList.size=0)}"> <i>No records to display.</i> </apex:pageBlock> . . . </messaging:attachment> </messaging:emailTemplate> 
+4
source share
3 answers

Generally speaking, wrap your code in an element of a higher page (for example, <apex:pageBlock> ), and then use the rendered attribute. It is optional and available on most elements of the page, the link to the component should provide you with a complete list of attributes supported for each tag.

In your case, I suppose something like this should do the trick:

 <apex:pageBlock rendered="{!AND(NOT(ISNULL(auditList)),auditList.size>0)}"> Stuff is in, put "repeat" tag here. </apex:pageBlock> <apex:pageBlock rendered="{!OR(ISNULL(auditList),auditList.size=0)}"> No records to display. </apex:pageBlock> 

Feel free to experiment with the syntax. I used function names, as in the formula editor (for formula fields, validation rules, etc.), but normal logical operators such as & &, || should also be available.

+4
source

Use a wrapper (my personal favorite) and use a formula that checks the list size for the rendered attribute.

 <apex:outputPanel rendered="{!relatedTo.Site_Audit__r.size = 0}"> No Records </apex:outputPanel> <apex:outputPanel rendered="{!relatedTo.Site_Audit__r.size != 0}"> <apex:repeat var="auditList" value="{!relatedTo.Site_Audit__r}"> ... </apex:repeat> </apex:outputPanel> 
+1
source

Stick to the wrapper (use apex: outputPanel or apex: variable) and create a method that returns the size of the list ie

 public Integer listSize{get { if(auditList != null) return auditList.size(); else return 0;}} 

Use this in a condition that determines visibility.

0
source

All Articles