5

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 :)

1 Answer 1

3
<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>
6
  • Yes I have used that. But was thinking of some better alternative if exist Dec 15, 2015 at 13:24
  • @MonalisaDas @Ratan Normally you can just use .size to get a number of the elements in the child relationship. Like {!obj.relation__r.size} But in this case it is not working :( Dec 15, 2015 at 13:27
  • @mast0r Aggree I found it is difficult with .size The only way I found this. Let see if any one have better option
    – Ratan Paul
    Dec 15, 2015 at 13:42
  • @Ratan I have just added rows="1" attribute to apex repeat so that it doesn't need to iterate over the whole long list rows. Dec 16, 2015 at 5:36
  • If you add rows="1" then it will count only one record.
    – Ratan Paul
    Dec 16, 2015 at 5:45

You must log in to answer this question.

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