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
pluviosillapluviosilla 

Problem Binding to Fields in a Child Object

I'm having trouble finding the right binding expression for a VF page. Here's a snippet:

 

<apex:pageBlock title="Example pageBlockTable">

    <apex:pageBlockTable value="{!accounts}" var="item">

 

THESE WORK:

 

            <apex:column value="{!item.name}"/>

            <apex:column value="{!item.owner.name}"/>

 

But NONE OF THESE REFERENCES TO CHILD FIELDS WORK

            <apex:column value="{!item.contracts.lastname}"

            <apex:column value="{!item.contract.lastname}"

            <apex:column value="{!item.lastname}"

            <apex:column value="{!item.contracts[lastname]}"/>       

 

</apex:pageBlockTable>

 

Here's my controller:

 

public class CtrlrTestDataTables {
    List<Account> accounts;
    public List<Account> getAccounts() {       

        if(accounts == null) accounts =

           [select name, owner.name,                              

                (select contact.firstname, contact.lastname from contacts)

                 from account];           

        return accounts;   

    }    

}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You have to use an apex:repeat tag. For example, this should work:

 

<apex:page>
  <apex:pageBlock>
    <apex:pageBlockTable value="{!Accounts}" var="Account">
      <apex:column headerValue="Account Name" value="{!Account.Name}"/>
      <apex:column headerValue="Contacts">
         <apex:repeat value="{!Account.Contacts}" var="Contact">
           {!Contact.FirstName} {!Contact.LastName}
         </apex:repeat>
      </apex:column>
    </apex:pageBlockTable>
</apex:page>

Make sure that you're querying all of the fields you reference in the object or you will get an error:

 

SELECT Id,Name,(SELECT Id,FirstName,LastName FROM Contacts) FROM Account

 

All Answers

sfdcfoxsfdcfox

You have to use an apex:repeat tag. For example, this should work:

 

<apex:page>
  <apex:pageBlock>
    <apex:pageBlockTable value="{!Accounts}" var="Account">
      <apex:column headerValue="Account Name" value="{!Account.Name}"/>
      <apex:column headerValue="Contacts">
         <apex:repeat value="{!Account.Contacts}" var="Contact">
           {!Contact.FirstName} {!Contact.LastName}
         </apex:repeat>
      </apex:column>
    </apex:pageBlockTable>
</apex:page>

Make sure that you're querying all of the fields you reference in the object or you will get an error:

 

SELECT Id,Name,(SELECT Id,FirstName,LastName FROM Contacts) FROM Account

 

This was selected as the best answer
pluviosillapluviosilla

YES!!!!!!!!!!!  Thanks very much for this.

 

I think it would be very useful for the docset to provide an example like yours in each of the table tag man pages (<apex:pageTableBlock>, <apex:dataTable> etc.) and not just in the page for <apex:repeat>.

sfdcfoxsfdcfox

I just realized that my wording was misleading in the prior response. You need to use any repeatable tag to list the child items. You may do so in any combination that Visualforce allows. For example, you could do this:

 

<apex:pageBlock>
    <apex:pageBlockTable value="{!accounts}" var="account">
        <apex:column>
            {!Account.Name}
        </apex:column>
        <apex:column>
            <apex:dataTable value="{!account.contacts}" var="contact">
                <apex:column>
                    <apex:facet name="header">
                        {!Contact.FirstName} {!Contact.LastName}
                    </apex:facet>
                </apex:column>
                <apex:column>
                    <apex:facet name="header">
                        {!Contact.Email}
                    </apex:facet>
                </apex:column>
            </apex:dataTable>
        </apex:column>
    </apex:pageBlockTable>
</apex:pageBlock>

You could use apex:repeat > apex:pageBlock > apex:pageBlockTable, or apex:pageBlock > apex:pageBlockTable > apex:column > apex:repeat, or most any combination of apex:repeat, apex:pageBlockTable, apex:dataTable, and/or apex:dataList, depending on the precise UI you plan on presenting.