function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Render Related List(s) only if a related record in the list exists

I'm trying to use visualforce to perform the following:
A. Show related lists (and all headings) only if a related record exists.
B. If a related record does not exist, the particular section is not displayed.
I'm trying to use the apex:relatedList tag with the rendered attribute.
Is it possible to check if the list is empty as a boolean expression within visualforce without having to create a custom controller method? I have a working solution, but if I want to add this functionality for all related lists, it's a lot of extra controller code to write.
Thanks in advance.
VisualForce View Page
<apex:page standardController="Account" extensions="AccountHiddenListController">
    <apex:detail relatedList="false">
        <apex:relatedList list="Opportunities" rendered="{!relatedOpportunitiesExist}">    
Controller Code:
public class AccountHiddenListController
    private final Account account;
    private boolean relatedOpportunitiesExist;
    public AccountHiddenListController (ApexPages.StandardController accountController)
        this.account = (Account) accountController.getRecord();
    public boolean getRelatedOpportunitiesExist()
        if (this.relatedOpportunitiesExist != null)
            return this.relatedOpportunitiesExist;
        List<Opportunity> opp = this.getOpportunitiesList();
        if (opp.size() > 0)
            this.relatedOpportunitiesExist = true;
            this.relatedOpportunitiesExist = false;       
        return this.relatedOpportunitiesExist;
    public void setRelatedOpportunitiesExist()
    public List<Opportunity> getOpportunitiesList()
        if (this.account == null)        
            return null;       
        return [select
        from Opportunity o
        where o.AccountId =];

Anybody have an update on this one?


The VF Developers Guide has an example, but it does not seem to work. 

The example: 



<apex:pageBlockTable value="{!results}" var="l"rendered="{!NOT(ISNULL(results))}">



I tried the ISNULL funtion and it did not work.



This is a bit of a cobbled together soloution but it can all be done inside the visualforce page without using any kind of custom apex controller



<apex:page standardController="Account" >
        <apex:repeat value="{!Account.Related_object__r}" var="temp" rows="1">
            <div class="row">
            <apex:relatedList list="Related_object__r"/>




Since the repeater is only returning one row you are able to disply the related list once...


I know how annoying this is and I spent a good 4 hours on this ;)






How about <apex:relatedList list="Related_object__r" rendered="{!Related_object__r.size > 0}"/>

Thanks Ofrag, that really works! You're cleverness just saved us 4 hours :)

Hey ,


On the standard detail page , I need a button that takes me to the detail record page of the Child record. I figured I could do this with a VF page

But Also want to create a new Child record if the Master doesnot have any child record.


I have been struggling for the last 3 days but cannot figure out a solution.


Please help.


Thanks in advance


Chris LanceChris Lance
Ofrag's solution works perfectly!