+ Start a Discussion

How to displaying a related records from a parent on a Visualforce detail page for a child?

I have a custom object (MyChild) that is a child of Account.  I'm building a custom Visualforce replacement for the detail view of MyChild.   I'm using the standard controller for MyChild.  I'd like to display a table on the top of the page dcontaining the list of Contact records associated with the parent Account.  I can make this work with a seperate Visualforce page iframed in as an sControl.  But I don't want to do that.  Does anyone have a pattern component or extension to pass a list of child records of another type back like this?

Here is the sample code for VF page and Controller. You might need to improvise it as per your requirement

- lesson__c is my custom object that is a child for Accounts
- Contacts is another child object to accounts

The output looks something like the below

User-added image

VF Page :

<apex:pageBlock >
<apex:page standardcontroller="lesson__c" extensions="lessonext" tabStyle="lesson__c">
<apex:pageblocksection title="Contacts" columns="1">
<apex:pageblocktable value="{!accounts}" var="cons1">
<apex:column value="{!cons1.id}"/>
<apex:column value="{!cons1.firstname}"/>
<apex:column value="{!cons1.lastname}"/>
<apex:pageblocksection title="Detail" columns="1">
<apex:detail />

Controller Extension class:

public class lessonext {

    public lessonext(ApexPages.StandardController controller) {


    public list<contact> cons;
    public List<Contact> getAccounts() {
    Id recid=apexpages.currentpage().getparameters().get('id');  
    Lesson__c lesson=[select id,Account__c from Lesson__c where id=:recid limit 1];
        cons=[select id,firstname,lastname from contact where Accountid=:lesson.Account__c];
        if(cons.size()>0){return cons;}    
        return null;


Please mark the reply as best post if it resolved the requirment.
Thanks Ramu, I think that will do it.  
khadhar shajahankhadhar shajahan
Visualforce ErrorHelp for this Page
System.QueryException: List has no rows for assignment to SObject 
Class.Lessonnext.getAccounts: line 13, column 1

I faced this error Brother
khadhar shajahankhadhar shajahan
I con't findout