How to check size of related records without using controller

I have a simple requirement in which I have used standard Controller Account in VF like this –

<apex:page standardController="Account">
  <apex:pageBlock >
   <apex:relatedList list="Contact">
        <apex:facet name="header">Titles can be overriden with facets</apex:facet>
    </apex:relatedList>
  </apex:pageBlock> 
</apex:page> <br />

Here I want to check whether the Account has related contacts or not without using controller that is I want to evaluate this logic at page level itself . But if try to use something like this – {!Account.Contacts.size}, I get

error message Invalid field ‘size’ for SObject ‘Contact’.

I even tried with following VF functions – ISBLANK and IF for checking whether its blank or null respectively but no help. Both returns false even if there is no related contact records. Any suggestions?
Thanks in advance 🙂

Answer

<apex:page standardController="Account">
  <apex:pageBlock >
    <apex:variable var="j" value="{!0}"/>
    <apex:repeat var="i" value="{!Account.Contacts}">
      <apex:variable var="j" value="{!j+1}"/>{!j}
    </apex:repeat>
    <script>
      alert('{!j}');
    </script>
    <apex:relatedList list="Contacts">
      <apex:facet name="header">Titles can be overriden with facets</apex:facet>
    </apex:relatedList>
  </apex:pageBlock> 
</apex:page> 

Use <apex:variable> and <apex:repeat> for calculate


Edit —–

<script>
   var countSize = 0;

   <apex:repeat var="i" value="{!Account.Contacts}">
       countSize+=1;
   </apex:repeat>

   alert(countSize);
</script>

Attribution
Source : Link , Question Author : Monalisa Das , Answer Author : Ratan Paul

Leave a Comment