Using Related Lists in Visualforce Pages

May 13th, 2009

Let’s face it, coding Visualforce pages is sometimes too easy. The tag-based syntax allows you to design effective UIs with very little effort. One of the more powerful components is the relatedList which lets you display a list of related records based upon a lookup or master-detail relationship. One of the major benefits of this tag, as opposed to writing your own with a dataTable, is that you can visually edit the related list with the Salesforce.com page layout editor. Any changes you make to the related list in the page layout editor (columns, sorting, etc.) will automatically be reflected in your Visualforce page.

One of the questions that I see often on the message boards is how to find the relationship name of the related list so that it can be coded in the Visualforce page. The related list portion of an Opportunity display Visualforce page may look like this:

1
2
3
4
5
6
 
<apex:relatedList list="OpportunityLineItems"/>
<apex:relatedList list="R00NR0000000URnZAGQ"/>
<apex:relatedList list="OpportunityContactRoles"/>
<apex:relatedList list="OpportunityTeamMembers"/>  
<apex:relatedList list="OpenActivities"/>

So let’s say you have an custom object named “Opportunity Thing” that has a lookup relationship to Opportunities (called Opportunity__c) and you would like to display this related list on the Opportunity display Visualforce page. To find relationship name, go to the Opportunity Thing custom object, click on Fields and then click your Opportunity field. You will see the lookup options at the bottom right of the screen.

relationship1

The Child Relationship Name is the name of the list attribute for the relatedList tag with “__r” appended to it. So your relatedList tag would look like:

1
2
 
<apex:relatedList list="Opportunity_Things__r"/>

You can also click the edit button at the top of the page and change the name of the relationship if you would like something a little friendlier.

relationship2

Categories: Salesforce, Visualforce

Leave a comment

Comments Feed6 Comments

  1. Doug

    Your pages are really helpful!

    I was trying to combine several of your postings and create a custom Add page for assets. I have it hooked up to a button, the button in the assets related list under accounts, and I’ve defined my VF page with

    apex:page standardController=”Asset” recordSetVar=”assets” tabStyle=”Asset” extensions=”Assets_Override”

    When I try to add a relatedList, however, VF insists that the list be a child of Accounts, not Assets. Huh?

  2. Keith

    Jeff,
    This is a pretty old post, but I’m new to VF. I have a page working that shows just the Opportunity Products in a list, and it’s a child page from a list of Cases -> Opportunities -> Opportunity Products.
    On that child page of just products, is it also possible to put the “documents” from the original Case object? Or because the controller is “Opportunity” for the page, is that impossible?

  3. Jeff Douglas

    If you use your own Visualforce page and a controller extension you should be able to display the documents for the original case. Take a look at the relatedList component and the subject attribute. You should be able to set that attribute to the case id.

  4. Jeff Douglas

    “When I try to add a relatedList, however, VF insists that the list be a child of Accounts, not Assets. Huh?” So is this related list on the Visualforce page you mention above with the Asset standardController?

  5. sreekumar menon

    Hi Jeff.
    I understand that we can drop a VF page within a page (eg case detail page) and we can write our own related lists.. but Iam not able to drop this VF page in the related lists section. any ideas?

  6. Jeff Douglas

    On a page layout, you can only drop the Visualforce page into the details section. Unfortunately you cannot place it in the related list section. See this page for more info.

Leave a comment

Feed

http://blog.jeffdouglas.com / Using Related Lists in Visualforce Pages

WordPress Appliance - Powered by TurnKey Linux