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
temhof4242temhof4242 

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?
Ramu_SFDCRamu_SFDC
Hi,

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:pageblocktable>
</apex:pageblocksection>
<apex:pageblocksection title="Detail" columns="1">
<apex:detail />
</apex:pageblocksection>
</apex:pageBlock>
</apex:page>

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;}    
        else{
        return null;
        }
     
      
    }

}

Please mark the reply as best post if it resolved the requirment.
TempleHoffTempleHoff
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