views:

216

answers:

3

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. 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 pseudo code I am looking for a test such as:

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 the help!


Update in response to first answer from 'eyescream'


Gave the apex:pageBlock method a shot, but ran into the following error when trying to save/deploy:

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

Now this is a email template that produces an attached PDF (see general outline of the code below). So is that the case...pageBlock is not allowed within a 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>
A: 

Generally speaking - wrap your code in higher page element (like <apex:pageBlock>) and then use the attribute rendered. It's optional and available on most of the page elements, the component reference should give you complete list of attributes supported for each tag.

In your case I suppose something like that 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've used the function names as in the formula editor (for formula fields, validation rules etc.) but normal logic operators like &&, || should be available too.

eyescream
I tried this out but ran into some issues. I edited my original post with my results and questions. Thanks for the help!!
src0010
Then use some other "content wrapping" element till you get rid of the error ;) Try `apex:outputPanel rendered="{!NOT(ISNULL(auditList))}"`, probably apex:outputText will be safe too.
eyescream
src0010
Seems like the test needs to be on the 'apex:repeat' itself or within it no? Like it is not recognizing the object when wrapped around the 'apex:repeat' since the var has not been declared yet?
src0010
No, I think it definitely should be outside "repeat". Maybe try with `{!relatedTo.Site_Audit__r}` as a field being tested then? Based on example provided by SF for `<messaging:attachment>` tag in the component reference I was able to create such template: http://dl.dropbox.com/u/709568/stackoverflow/emailtemplate.txt I didn't try it but at least it compiles ;)
eyescream
src0010
src0010
A: 

Stick with the wrapper (use apex:outputPanel or apex:variable) and create a method that returns the list size i.e.

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

Use this in the conditional that determines visibility.

weesilmania
weesilmania, thanks for the additional suggestion. I may give this route a shot if what we are trying in A#1 doesn't work...but so far we are making some progress.
src0010
src0010
A: 

Use a wrapper ( is my personal favorite), and use a formula that checks the size of the list 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>
Ralph Callaway
Hi Ralph, thanks for the suggestion. When I try that I get the following error when deploying: "Result: FAILED Problem: Invalid field 'size' for SObject 'Site_Audit__c'". They also suggested this same method on the SF forums in this thread (http://goo.gl/ubix), but I was unable to ever get it working. Seems to work for everyone else except me :-). Any idea why?
src0010

related questions