+ Start a Discussion
M ColoM Colo 

Referencing sub query in pageblocktable

I have 2 objects, contacts(parent) and custom_1(child).  Im pulling info from both objects and need to display it in a VF table.  So in the controller I have the following query

 

exportGuestList = Database.query(

'select Salutation, FirstName, LastName, Email, Phone, MobilePhone, Company_Name__c, Title, Business_Address__c, Alternate_Address__c, GroupName__c  '+

 

', (SELECT Addres__c FROM Custom_1__r)'+

 

' from Contact where Deceased__c = false AND Id IN(select Custom_2__c '+

 

' from Custom_3__c where Custom_2__c = \''+

whereSQLContactId +

'\') order by LastName ASC LIMIT 5000');

 

I have no problems rendering the contact fields in the table, but when I attempt to render the addres__c field, like so:

 

<apex:pageBlockrendered="{!NOT(ContactsCount==0)}"title="Contacts:">

<apex:pageBlockTablevalue="{!exportGuestList}"var="selecCont">

<apex:columnvalue="{!selecCont.Salutation}"/>

<apex:columnvalue="{!selecCont.LastName}"/>

<apex:columnvalue="{!selecCont.FirstName}"/>

<apex:columnvalue="{!selecCont.Email}"/>

<apex:columnvalue="{!selecCont.Phone}"/>

<apex:columnvalue="{!selecCont.MobilePhone}"/>

<apex:columnvalue="{!selecCont.Company_Name__c}"/>

<apex:columnvalue="{!selecCont.Title}"/>

<apex:columnvalue="{!selecCont.Business_Address__c}"/>

<apex:columnvalue="{!selecCont.Addres__c}"/>

 

I get the following Error:

Error: Invalid field Addres__c for SObject Contact

 

How can I specify in the VF page the proper reference to Custom_1

 

If I append Custom_1__r.  to Addres__c in the table I get:

 

Error: Unknown property 'VisualforceArrayList.Addres__c'
 
Somewhat new to SOQL and apex so i've been pretty stumped on this.
 
Thank you
Best Answer chosen by Admin (Salesforce Developers) 
Andrew WilkinsonAndrew Wilkinson

You need to do a nested table inside of your table.

 

 

For the last column do this instead.

 

 

<apex:column>

    <apex:pageBlockTable value="{!selecCont.Custom_1__r}"  var="custom">

        <apex:column value="{!custom.Addres__c}"/>

    </apex:pageBlockTable>

</apex:column>

 

You don't have to use another pageBlocktable. You could use an apex:repeat tag instead. But you have to iterate over the subquery since there can be more than one record in the subquery.

All Answers

Andrew WilkinsonAndrew Wilkinson

You need to do a nested table inside of your table.

 

 

For the last column do this instead.

 

 

<apex:column>

    <apex:pageBlockTable value="{!selecCont.Custom_1__r}"  var="custom">

        <apex:column value="{!custom.Addres__c}"/>

    </apex:pageBlockTable>

</apex:column>

 

You don't have to use another pageBlocktable. You could use an apex:repeat tag instead. But you have to iterate over the subquery since there can be more than one record in the subquery.

This was selected as the best answer
M ColoM Colo

It worked, thanks so much!

been banging my head against this wall for a while now.

PrasadVRPrasadVR

Hi Andrew,

 

       Could you please explain how to show both query and subquery records in a single pageblock table, I don't want to use nested tables i want show all in a single table.

 

Suppose if i use repeat means it will show every record in one table, How to avoid

 

Is It possible to Show all in one place.