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
JoyDJoyD 

way to group a list of Events by Assigned To?

I have a List of Events output to a VF page that I would like to group by the Assigned To person.  Right now, I just have that field output to one of the columns - but ideally would like to Group By just like a Report.  Currently using a PageBlockTable to output - but am open to whatever might work.  Is there a way to do this - I can't seem to find anywhere?

 

I would assume my custom controller would need to somehow handle this, but I can't seem to understand how to make that work.  Here's my SOQL query if it helps in the understanding at all:

 

SELECT Who.Name, WhatId, StartDateTime, Result__c, Owner.Name, LastModifiedDate, LastModifiedBy.Name, Appointment_Type__c, ActivityDateTime, ActivityDate, Subject
                FROM Event
                WHERE Result__c != 'Cancelled' 
                	AND Result__c != 'No Show' 
                	AND ActivityDate >= :firstDateToUse
                	AND ActivityDate <= :secondDateToUse
                ORDER BY Owner.Name, ActivityDateTime

 

 

The VF page lists off all the information for someone to update the Results of all the appointments yesterday.  Currently, we do this by going to a Report, opening each appointment, updating it, going back to Report...etc.

 

Thanks in advance!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The apex:repeat tag simply allows you to iterate through a list.  Thus if you have a list of classes that contains a list, you can iterate in a nested fashion.

 

For example, if you have a wrapper class 'MyWrap' that contains a list of sobjects 'MyObjectList', and a method in your controller that returns a list of these - MyWrapList - you can iterate in the following way:

 

 

<table>
   <apex:repeat value="{!MyWrapList}" var="MyWrap">
     <!-- "heading" row for mywrap object instance -->
     <tr><td colspan="3">{!MyWrap.Name}</td></tr>
     <!-- iterate the contents of mywrap list -->
     <apex:repeat value="{!MyWrap.MyObjectList}" var="MyObject>
       <tr>
          <td>Field 1 = {!MyObject.Field1__c}</td>
          <td>Field 2 = {!MyObject.Field1__c}</td>
          <td>Field 3 = {!MyObject.Field1__c}</td>
       </tr>
     </apex:repeat>
   </apex:repeat>
</table>

 

 

All Answers

bob_buzzardbob_buzzard

There's a number of ways you could achieve this.  My personal favourite is to use a wrapper class.  

 

Something like the following should work:

 

Create an Apex class that has a field containing the assigned to name and a list of events that are assigned to that person.

 

In your custom controller, create a list of these classes based on  your query - iterate the results and create a new instance of the wrapper class when the assigned to name changes.  Then create a property that exposes this list.

 

In the VF page you can then use apex:repeat tags in conjunction with HTML tables to (for example) render a new table for each assigned to.

JoyDJoyD

Sorry, I'm still new at this (just got back from the DEV 501 class), and not quite able to follow.  I think I understand how the wrapper class will work (will need to figure out how to iterate through the List returned from the query to get them in the wrapper class correctly, but seems understandable), but I'm kind of baffled as far as the VF side will look.

 

Are you saying I'd need to construct my own HTML table instead of using any of the standard VF components like pageBlockTable?  I haven't seen use of the repeat tag yet, and the Standard Component Reference isn't really helping me understand how I would do this.  Are you able to nest these repeat tags?

 

Below is the example from the Standard Component Reference - so is there a way I could wrap the data sections in one repeat tag - then wrap the whole thing from *** to *** in another repeat that would be iterating through the wrapper class?

 

    <table border="0" >
***can i put another repeat here to iterate through wrapper class?***
        <tr>
            <th>Case Number</th><th>Origin</th>
            <th>Creator Email</th><th>Status</th>
        </tr>
        <apex:repeat var="cases" value="{!Account.Cases}">
        <tr>
            <td>{!cases.CaseNumber}</td>
            <td>{!cases.Origin}</td>
            <td>{!cases.Contact.email}</td>
            <td>{!cases.Status}</td>
        </tr>
        </apex:repeat> 
***end wrapper repeat tag?***
    </table>

 

 

Ideally - the Assigned To person would have their own, perhaps colored bar going across...and then their data below.

 

I really appreciate the help!  Sorry if this is super-basic stuff!

bob_buzzardbob_buzzard

The apex:repeat tag simply allows you to iterate through a list.  Thus if you have a list of classes that contains a list, you can iterate in a nested fashion.

 

For example, if you have a wrapper class 'MyWrap' that contains a list of sobjects 'MyObjectList', and a method in your controller that returns a list of these - MyWrapList - you can iterate in the following way:

 

 

<table>
   <apex:repeat value="{!MyWrapList}" var="MyWrap">
     <!-- "heading" row for mywrap object instance -->
     <tr><td colspan="3">{!MyWrap.Name}</td></tr>
     <!-- iterate the contents of mywrap list -->
     <apex:repeat value="{!MyWrap.MyObjectList}" var="MyObject>
       <tr>
          <td>Field 1 = {!MyObject.Field1__c}</td>
          <td>Field 2 = {!MyObject.Field1__c}</td>
          <td>Field 3 = {!MyObject.Field1__c}</td>
       </tr>
     </apex:repeat>
   </apex:repeat>
</table>

 

 

This was selected as the best answer
JoyDJoyD

Thank you very much for your help!  I played and played with it all last night and did finally come up with a solution very nearly what you have provided so thanks for the confirmation I did it right!  :-)

ethanoneethanone

Thanks for the post. I have a similar demo I'm constructing. I've got a series of links each of which are in a group (from a pick list) like so:

|Group  |Name  |URL           |
|-------|------|--------------|
|Group A|Google|www.google.com|
|Group A|Yahoo |www.yahoo.com |
|Group B|MSN   |www.msn.com   |
|Group B|Yelp  |www.yelp.com  |
|Group B|CNN   |www.cnn.com   |

 I want the information to display on the page such that each group is only listed once in a header row.

 

So my question is: how would I write the controller so that for each group, the list of links is listed?

 

I'm trying to avoid executing a query for each group as it seems it will hit govenor limits. If I return a set of groups and a list of links, then I still need to tie them together some how either on the VF page or in the controller and I'm not sure how to do that. Can you provide more explanation to the solution below?