[SalesForce] Use apex:relatedlist in visual force component

This is my VF page "TestPage":

<apex:page standardController="Account">
<c:TestComponent recordId="{!account}" />
</apex:page>

Visual Force component "Test Component":

<apex:component controller="Taccount">
<apex:attribute name="recordId" description="Record Id to show field history for." type="sobject"  assignTo="{!record}" required="true"/>
 <apex:relatedList list="Opportunities"/> 
<apex:relatedList list="record.Opportunities"/> 
</apex:component> 

Problem is that <apex:relatedList list="Opportunities"/> or <apex:relatedList list="record.Opportunities"/> is not showed in the main page"TestPage".

Do you know how can i show, in the main page, a related list added in a component?

Thanks in advance for any advice.

Best Answer

You can do this by setting the subject attribute of apex:relatedList.

For example:

The component can be written to accept the account record as an attribute passed into it.

<apex:component>
  <apex:attribute name="acctRec" type="Account" description="The Account passed in"/>
  <apex:relatedList subject="{!acctRec}" list="Opportunities"/>
</apex:component>

The VF Page can pass the account record.

<apex:page standardController="Account">
    <c:testComponent acctRec="{!account}" />
</apex:page>

The result will be that the Opportunities for the Account specified by the id parameter will be displayed within the component. Opportunities related list

Related Topic