• Ofrag
  • NEWBIE
  • 10 Points
  • Member since 2010

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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}">    
        </apex:relatedList>
    </apex:detail>
</apex:page>
 
 
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;
        }
        else
        {
            this.relatedOpportunitiesExist = false;       
        }
        return this.relatedOpportunitiesExist;
           
    }
   
    public void setRelatedOpportunitiesExist()
    {
   
    }
   
    public List<Opportunity> getOpportunitiesList()
    {
        if (this.account == null)        
            return null;       
        return [select o.id
        from Opportunity o
        where o.AccountId = :account.id];
       
    }
   
}