0

I have created a list in visualforce but it's quite boring. I would like to group by Account so it would look like

HgCapital
Month    Subject          Activity Date
Feb      blah blah blah   01/02/2015
Apr      blah blah blah   15/04/2015
Aug      blah blah blah   04/08/2015

Instead of

Month    Subject    Account    Activity Date
Feb      Blah blah  HgCapital  01/02/2015

I'm thinking this can be done in Visualforce and I tried creating nested pageBlocks to no avail.

Huge thanks in advance for your help.

APEX

public with sharing class FutureMeetings_CC {
    public List<Event> getFutureList()  {
        return [SELECT Id, AccountId, Subject, ActivityDate
                        FROM Event 
                        WHERE type='Meeting' AND ActivityDate > TODAY
                        AND AccountId  IN 
                            (select id FROM Account 
                                WHERE Status__c = 'Active Prospect')
                                ORDER BY ActivityDate ASC];


    }


}

VisualForce

<apex:page Controller="FutureMeetings_CC">

    <apex:pageBlock title="Future Meetings">

        <apex:pageBlockTable value="{!FutureList}" var="Meeting">

            <apex:column >
                <apex:facet name="header">Month</apex:facet>
                <apex:outputText value="{0, date, MMMM}">
                    <apex:param value="{!Meeting.ActivityDate}"/>
                </apex:outputText>
            </apex:column>

            <apex:column >
                <apex:facet name="header">Account Name</apex:facet>
                <apex:outputField value="{!Meeting.AccountId}"/>
            </apex:column>

            <apex:column >
                <apex:facet name="header">Subject</apex:facet>
                <apex:outputField value="{!Meeting.Subject}"/>
            </apex:column>

            <apex:column >
                <apex:facet name="header">Date</apex:facet>
                <apex:outputField value="{!Meeting.ActivityDate}"/>
            </apex:column>

        </apex:pageBlockTable>

    </apex:pageBlock>

</apex:page>
  • 1
    It would be helpful if you added more detail. Can you actually share with us the data structure you are looping through? Are these SObject records? Some other form of data? How would they be grouped by Account? What is their relationship to it? – Adrian Larson Dec 2 '15 at 16:52
  • Sorry. I've added the code above. – neo2049 Dec 2 '15 at 17:21
0

Solution

public with sharing class FutureMeetings_CC {


     public List<Account> getFutureList(){

        List<Account> lst = [select id,Name,(SELECT Id, Subject, ActivityDate FROM Events 
                                          WHERE type='Meeting' AND ActivityDate > TODAY ORDER BY ActivityDate ASC) 
                               FROM Account WHERE Status__c = 'Active Prospect'];
         return lst;
     }
}

Page

<apex:page Controller="FutureMeetings_CC">
<apex:form>
    <apex:pageBlock title="Future Meetings">

        <apex:repeat value="{!FutureList}" var="Account">
        <apex:outputField value="{!Account.Name}"/>
        <apex:pageBlockTable value="{!Account.Events}" var="Meeting">

            <apex:column >
                <apex:facet name="header">Month</apex:facet>
                <apex:outputText value="{0, date, MMMM}">
                    <apex:param value="{!Meeting.ActivityDate}"/>
                </apex:outputText>
            </apex:column>

            <apex:column >
                <apex:facet name="header">Subject</apex:facet>
                <apex:outputField value="{!Meeting.Subject}"/>
            </apex:column>

            <apex:column >
                <apex:facet name="header">Date</apex:facet>
                <apex:outputField value="{!Meeting.ActivityDate}"/>
            </apex:column>

        </apex:pageBlockTable>
       </apex:repeat>
    </apex:pageBlock>
</apex:form>
</apex:page>
  • Curious, why do you have public set for your properties? These do not seem like they should be writable from the page. – Adrian Larson Dec 2 '15 at 17:51
  • yes we can use without that – Ratan Paul Dec 2 '15 at 17:51
  • It's pretty easy to auto-pilot while creating properties and just give them public setters. Completely unnecessary, but there are a lot less callers involved in Apex than there are in traditional Java, so I find that devs tend to care less about visibility (beyond whether or not something should be global). – Christian Carter Dec 2 '15 at 17:55
  • Yes but I think your intent as a developer is much more clear when you use private setters everywhere except where you want people interacting with your properties. – Adrian Larson Dec 2 '15 at 17:56
  • @AdrianLarson Added both option – Ratan Paul Dec 2 '15 at 18:03
0

First of all, I would add some lazy loading so you do not need to run the query every time you access a property. The basic pattern looks like:

public List<Account> accounts
{
    get
    {
        if (accounts == null)
        {
            accounts = [SELECT ... FROM Account WHERE ...];
        }
        return accounts;
    }
    private set;
}

As @ratan mentioned, you can use a sub-query to get everything you need in one go:

SELECT Name, (SELECT Subject, ActivityDate FROM Events)
FROM Account WHERE ...

It might also be worth splitting out these tables into a VisualForce Component:

<apex:component>
    <apex:attribute name="account" type="Account"
        description="Account for which to display events" />
    <apex:pageBlockSection title="{!account.Name}">
        <apex:pageBlockTable value="{!account.Events}" var="event">
            <apex:column>
                <apex:facet name="header">Month</apex:facet>
                <apex:outputText value="{0, date, MMMM}">
                    <apex:param value="{!event.ActivityDate}"/>
                </apex:outputText>
            </apex:column>
            <apex:column value="{!event.Subject}" />
            <apex:column value="{!event.ActivityDate}" />
        </apex:pageBlockTable>
    </apex:pageBlockSection>
</apex:component>

Then your VisualForce Page markup would essentially boil down to:

<apex:pageBlock>
    <apex:repeat value="{!accounts}" var="account">
        <c:EventsTable account="{!account}" />
    </apex:repeat>
</apex:pageBlock>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.