+ Start a Discussion
Ron WildRon Wild 

Testing size of lists or related item lists in Visualforce

I've been trying to MacGyver a way to hide an apex:repeat block if no items are found in the repeat list but haven't figured out a way to do it.

 

Ideally, there would be a COUNT() function that could be used to make rendering of an apex:outputPanel or other tag conditional.  e.g. render="{!COUNT(list)>0}" but such a function doesn't exist yet  (Vote for my idea here: https://sites.secure.force.com/success/ideaView?c=09a30000000D9xtAAC&id=08730000000KX2uAAG )

 

The function would be used as follows:

 

 

<apex:outputPanel rendered="{!COUNT(contact.related_items__r)>0}" >

<h2>List Heading</h2>
<apex:repeat value="{!contact.related_items__r}" var="item">

</apex:repeat>

</apex:outputPanel>

 

 

Has anyone come up with a workaround that might work in lieu of a COUNT() function?

 

Thanks,

Ron

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Cory CowgillCory Cowgill

Here is how I've done this before on an VF Oppty Page for Tasks:

<apex:outputPanel rendered="{!IF(currOppty.Tasks.size > 0,true,false)}">

 

You should be able to do same thing for your related objects.

 

All Answers

Cory CowgillCory Cowgill

Here is how I've done this before on an VF Oppty Page for Tasks:

<apex:outputPanel rendered="{!IF(currOppty.Tasks.size > 0,true,false)}">

 

You should be able to do same thing for your related objects.

 

This was selected as the best answer
dfpittdfpitt

Hello,

 

I'm trying to use this with a related object but its not working.

 

If i write: <apex:outputPanel rendered="{!IF(relatedTo.Diagnosticos_Cita__r.size > 0,true,false)}">

 

I get this error: Error: Invalid field 'size' for SObject 'Diagnostico_Paciente__c'.

 

If I write: <apex:outputPanel rendered="{!IF(relatedTo.Diagnosticos_Cita.size > 0,true,false)}">

 

I get this error: Error: Invalid field Diagnosticos_Cita for SObject Cita__c

 

Any ideas on what mistake I'm making?

LinvioIncLinvioInc

You can't use ".size" in the context of a visualforce tag like that.  You'll have to store the size in a variable in your controller first, and then test it in the visualforce tag.

 

- R