Find your content:

Search form

You are here

Accessing child records in visualforce

 
Share

I have an object Feedback_Comment__c which lookups to Feedback__c. How can I access "Feedback_Comment__c" fields in a datatable column? I could pull all Feedback__c fields like this...

    <apex:column >
       <apex:facet name="header">Description (Click SAVE below after editing)</apex:facet>
       <apex:inputtext value="{!account.Description__c}" size="100" id="PanelId"/>
    </apex:column>

Here's what I have tried unsuccessfully...

   <apex:dataTable value="{!accounts}" var="account" id="theTable" ">
   <apex:column >
        <apex:facet name="header">FBC</apex:facet>
        <apex:outputLink value="{!Feedback_Comment__r.id}" id="theLink12">Comment Page</apex:outputLink>
    </apex:column>

-----------------controller------------------------

public class dataTableCon {
List<Feedback__c> fb;
List<Feedback_Comment__c> fbc;
 public List<Feedback__c> getAccounts() {
    if(fb== null) fb= [select Name, Description__c, Importance__c, Resolution__c, Status__c, Owner.name from Feedback__c];
    return fb;
}

Please guide me. Thanks


Attribution to: MnZ

Possible Suggestion/Solution #1

Since it appears that your issue is not knowing how to query for the parent & child records and then display them in the page - that is where my example begins:

Controller

This code assumes that your Feedback_Comment__c object has a relationship name of Feedback_Comments to its parent.

public with sharing class dataTableCon {

    public List<Feedback__c> getFeedbackRecords() {

        // this query needs a where clause, don't leave it unbounded
        List<Feedback__c> feedbackRecords = [SELECT Name
                                                , Description__c
                                                , Importance__c
                                                , Resolution__c
                                                , Status__c
                                                , Owner.Name
                                                , (SELECT Id
                                                        , Name
                                                        , CreatedDate
                                                    FROM Feedback_Comments__r)
                                            FROM Feedback__c];
        return feedbackRecords;
    }
}

Page

In this basic example, the table is iterating over Feedback records and then within each record there is a list of Feedback Comment records as children, per the query above. As such, one of the columns has a repeater within it to iterate over the children in the row.

<apex:page controller="dataTableCon">
    <apex:pageBlock>
        <apex:pageBlockTable value="{!FeedbackRecords}" var="feedbackRecord">
            <apex:column value="{!feedbackRecord.Name}" />
            <apex:column value="{!feedbackRecord.Description__c}" />
            <apex:column value="{!feedbackRecord.Importance__c}" />
            <apex:column value="{!feedbackRecord.Resolution__c}" />
            <apex:column value="{!feedbackRecord.Status__c}" />
            <apex:column value="{!feedbackRecord.Owner.Name}" />
            <!-- This column will have a list of the children comments inside the cell -->
            <apex:column>
                <apex:facet name="header">Comments</apex:facet>
                <!-- iterate over the list of children in this row -->
                <!-- the markup in the cell will be an unordered list -->
                <ul>
                    <apex:repeat value="{!feedbackRecord.Feedback_Comments__r}" var="feedbackComment">
                        <li><apex:outputField value="{!feedbackComment.Name}" /></li>
                        <li><apex:outputField value="{!feedbackComment.CreatedDate}" /></li>
                    </apex:repeat>
                </ul>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Attribution to: Mark Pond
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/33132

My Block Status

My Block Content