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
Sandeep PatwardhanSandeep Patwardhan 

Nested pageblocksection

Hi,
I am trying to acheive the following
I have a SOQL query - select Team Name, Team Lead and employees from Company
now team name and team lead are bound together. So I want to create a table in visualforce where team Name and Lead will show in header(pageblocksection) and respetive employee details will show in below table.
Team Name - Test - Lead - John Doe (table will show details of employees from that team)
Team Name - Test_1 - Lead - Tom Doe (table will show details of employees from that team) etc

NagendraNagendra (Salesforce Developers) 
Hi Sandeep Patwardhan,

Please find the below example matching your requirement criteria.

Scenario:
I will have to display a list of Accounts. Under each account i will have to display the associated Account Team Members.

SOQL Query:
The SOQL Query to retrieve both the Account Details as well as the Account Team Member details will be as follows..
 
Select Id,(Select TeamMemberRole, User.Name From Account.AccountTeamMembers), Name, BillingCountry from Account

This is what is called a nested query which is quite obvious from the fact that you can see two "Select" statements in a single query.

Now, let us create a simple visual force page to display the query results.

Visualforce Page:
<apex:page tabstyle="Account" controller="nestedqueryexample">
    <apex:pageblock> 
        <apex:pageblocktable value="{!accsandtmember}"  var="accdet">
            <apex:column >
                   <apex:facet name="header">                                        
                         Team Members                                                                               
                    </apex:facet>
                    <apex:pageblocktable value="{!accdet.AccountTeamMembers}" var="tm">
                        <apex:column headerValue="Team Member">
                               <apex:outputfield value="{!tm.User.Name}"/>
                         </apex:column>
                         <apex:column headerValue="Role">
                               <apex:outputfield value="{!tm.TeamMemberRole}"/>
                         </apex:column>                          
                    </apex:pageblocktable>
            </apex:column>
            <apex:column headervalue="Account Name">
                    <apex:outputtext value="{!accdet.Name}"/>
            </apex:column>
            <apex:column headervalue="Billing Country">
                    <apex:outputtext value="{!accdet.BillingCountry}"/>
            </apex:column>
        </apex:pageblocktable>
    </apex:pageblock>
</apex:page>

Controller (Apex Class):
 
public class nestedqueryexample
{
  public List<Account> getaccsandtmember()
  {
      List<Account> accounts = [Select Id,(Select TeamMemberRole, User.Name From Account.AccountTeamMembers), Name, BillingCountry from Account];
      return accounts;
  }
}

Your final output will look like this.

Screen shot for the above code

Please mark my solution as best answer if it helps yo.

Best Regards,
Nagendra.P