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
spatel_cespatel_ce 

Group Records in PageBlockTable

Hi,

 

I am looking for possible solution to get this result:

 

                          Account Name                                      Contact Name

                     United Oil & Gas Corp.                               Lauren Boyle

                     United Oil & Gas Corp.                               Avi Green

                     sForce                                                            Jake Llorrac

                     University of Arizona                                    Jane Grey

                     University of Arizona                                    Arthur Song

 

 

 Though, I am getting this result with my regular code:

                    

                         Account Name                                       Contact Name

                    United Oil & Gas Corp.                                 Lauren Boyle

                                                                                              Avi Green

                    sForce                                                             Jake Llorrac

                    University of Arizona                                     Jane Grey

                                                                                              Arthur Song

 

 

Now, here is my code on Visualforce Page:

 

<apex:pageblocksection id="pbsAccnt" rendered="true">
    <apex:pageBlockTable id="pbtAccnt" value="{!lstACcount}" var="AC">
        <apex:column headerValue="Account(s)" value="{!AC.Accnt__c}" />
        <apex:column headerValue="Contact(s)" value="{!AC.Cont__c}" />
    </apex:pageBlockTable>
</apex:pageblocksection>

 

I already tried with Wrapper class with <apex:repeat> but it is not working.

 

Any help would be greatly appreciated.

Best Answer chosen by Admin (Salesforce Developers) 
Devendra@SFDCDevendra@SFDC

Hi,

 

This blog from Bob Buzzard will help you to display Account and Its Contact records.

 

Thanks,

Devendra

All Answers

Devendra@SFDCDevendra@SFDC

You can retrieve Contact records by keeping ORDER BY on Account Name.

 

List<Contact> lstContact = [Select Id, AccountId, Account.Name, LastName from Contact Order BY Account.Name];

 

and display this list in PageBlockTable.

 

Thanks,

Devendra

 

 

spatel_cespatel_ce

Thanks Devendra... That worked. ORDER BY was totally skipped from my mind.

spatel_cespatel_ce

Hi,

 

I am getting result with helping your query, but I am still getting Account name repeated. So, is there any way to display just Account name in single line with all related records?? It can be different columns but no repeating account name.

Devendra@SFDCDevendra@SFDC

Hi,

 

This blog from Bob Buzzard will help you to display Account and Its Contact records.

 

Thanks,

Devendra

This was selected as the best answer
spatel_cespatel_ce

Thanks Devendra.

Jennifer Dos Reis ICOJennifer Dos Reis ICO

I think you could use this idea for gouping records by a particular field in a table. 

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000kGikIAE

This is the result of grouping contacts by account. 
example table