2

Is there a way to display a message if the list has 0 records? in VisualForce Page? so my idea is to display something like this in the Pageblock

 <apex:pageBlock title="Contacts">
      There are no records to display {!SelectedContacts.size=0}.
   </apex:pageBlock>

here is my visual force page actually I have:

<apex:pageblock id="Selected_PBS" title="Selected Contact"  >
  <apex:pageBlockTable value="{!SelectedContacts}" var="contact">
     <apex:column value="{!contact.Name}"/>
     <apex:column value="{!contact.Email}"/>
     <apex:column value="{!contact.Phone}"/>
  </apex:pageBlockTable>

3 Answers 3

2

Instead of using two pageBlocks you can conditionally render the title value in apex:pageBlock to show the availability and unavailability of Objects in list. I have modified only the title attribute in your code.

<apex:pageblock id="Selected_PBS" title="{!IF(SelectedContacts != NULL && SelectedContacts.size > 0,'Selected Contact','There are no records to display')}">
  <apex:pageBlockTable value="{!SelectedContacts}" var="contact">
    <apex:column value="{!contact.Name}"/>
    <apex:column value="{!contact.Email}"/>
    <apex:column value="{!contact.Phone}"/>
  </apex:pageBlockTable>
</apex:pageBlock>

Hope it helps.

4
  • The value 'null' is not valid for operator '>' Error is in expression '{!IF(SelectedContacts.size > 0,'Selected Contact','There are no records to display')}' in component <apex:pageBlock>
    – Nick
    Jul 17, 2015 at 17:22
  • My Bad. I assumed SelectedContacts will not be null and just checked its size. Now added a null check as well and updated my answer. Jul 17, 2015 at 17:26
  • 1+ is there a way to avoid render the pageblocktable? if the SelectedContact size = 0?
    – Nick
    Jul 17, 2015 at 17:29
  • 1
    use rendered="{!IF(SelectedContacts != NULL && SelectedContacts.size > 0,true,false)}" in pageblocktable Jul 17, 2015 at 17:33
2

You can create a <apex:outputText> and if the list size is 0 render it.

<apex:pageblock id="Selected_PBS" title="Selected Contact"  >
 <apex:outputText value="There are no records to display" rendered="{!SelectedContacts.size==0}" />
  <apex:pageBlockTable value="{!SelectedContacts}" var="contact">
     <apex:column value="{!contact.Name}"/>
     <apex:column value="{!contact.Email}"/>
     <apex:column value="{!contact.Phone}"/>
  </apex:pageBlockTable>

You can check about <apex:outputText> in salesforce Documentation.

2
  • <apex:outputText requires to have <apex:page> but my question is how can I incorporate the change in my existing code?
    – Nick
    Jul 17, 2015 at 16:42
  • <apex:outputText> simplest answer @vikas Can you please add a curly bracket in the starting of rendered attribute and / in the end of tag. Just to make it perfect! :)
    – Seesh
    Jul 17, 2015 at 17:06
1

Yes, use rendered:

<apex:pageBlock title="Contacts" rendered="{!SelectedContacts.size == 0}">
    There are no records to display
</apex:pageBlock>

Or:

<apex:pageBlock title="Contacts">
    <apex:outputPanel rendered="{!SelectedContacts.size == 0}">
    There are no records to display
    </apex:outputPanel>
    This text will always show.
</apex:pageBlock>
1
  • did not show the message and i have tried both
    – Nick
    Jul 17, 2015 at 16:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .