3

This is my VF page:

<apex:page standardController="Account">
<apex:relatedList list="Contacts" />
</apex:page>

I would like to show number of records in the related list. I´ve tried, without success, with:

{!Account.Contacts.size}

How can i accomplish this?

UPDATE: I know that i can do it in a controller extension (query contacts + size on result list) but i would like to calculate the number of record directly in the VF page.

Thanks in adavantage for any advice

  • Workaround: you could add a custom numeric field to Contacts that always displays "1" and then do a roll up summary on the account object of that field on Contacts. – user9959 Oct 10 '14 at 7:34
  • 1
    Thanks Tonino but using a roll up is a quite heavy solution. It will be better to count number of records in the controller with a query, but i would like to calculate the number directly in the VF page. – Enry Oct 10 '14 at 8:44
  • Ok..I think maybe this is not possibile (in VF page). – user9959 Oct 10 '14 at 8:47
6

This is very simple. You can try this code. I tested, it is working:

You need to use apex:repeat tag to get count

<apex:page standardController="Account">
    <apex:variable var="count" value="{!0}"/>
    <apex:repeat value="{!Account.contacts}">
        <apex:variable var="count" value="{!count + 1}"/>
    </apex:repeat>
    size is {!count}
    <apex:relatedList list="Contacts" />
</apex:page>
| improve this answer | |
1

Here's a nasty workaround that works in later API versions.

It works by coercing the related items to a string and measuring the length.

(Note that standard controller related items are not just SObject lists... so treat as black box)

count-related-list-items

| improve this answer | |
  • 1
    Works as expected and cool – Cray Kao Sep 24 '18 at 7:20
-2

I know it's been years since this question has been asked. But I think it could help others if I answer it anyway.

Use length instead of size in your markup.

{!Account.Contacts.length}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.