-2

I have a SF object TableA__c which has 3 fields Sales_Rep__c, Month__c, Sales_Revenue__c (needs to be aggregated).

I want to create a VF page which would have all the reps in rows and their aggregate revenue in corresponding months (months in column). However, I am not sure how I can do this.

3
  • 3
    Is there any reason why you can't use a standard summary report? That would simplify a lot of the process. What SOQL queries have you tried to create this?
    – Mike Chale
    Commented Jan 9, 2014 at 19:00
  • 1
    @MikeChale matrix, not summary ;) Robin - check the images at salesforce.stackexchange.com/questions/14561/…, something like that? It's perfectly doable with matrix reports.
    – eyescream
    Commented Jan 9, 2014 at 19:05
  • Thanks Mike and eyescream. I am creating a visual force page which has other functionality along with this table. So I have to create this in visualforce. It will be great if you can help me with how to code this in controller and VF page..Thanks
    – Robin
    Commented Jan 10, 2014 at 5:53

1 Answer 1

0

Below is some example code to allow you to do this. It is only basic skeleton code and you may want to improve it using a wrapper class for the object rather than the maps, but it is a brief functional piece. In general code solutions like this are not posted on the exchange.

You controller would be something like:

public with sharing class SalesRevenueController
{
    public Map<String, Map<String, Decimal>> salesRevenueDetails {get; set;}

    public SalesRevenueController()
    {
        salesRevenueDetails = new Map<String, Map<String, Decimal>>();

        List<AggregateResult> results = [SELECT SUM(Sales_Revenue__c), Sales_Rep__c, Month__c FROM SalesRevSummary__c GROUP BY Sales_Rep__c, Month__c];
        for(AggregateResult result : results)
        {
            if(salesRevenueDetails.get((String)result.get('Sales_Rep__c')) == null)
            {
                salesRevenueDetails.put((String)result.get('Sales_Rep__c'), new Map<String, Decimal>());
            }

            salesRevenueDetails.get((String)result.get('Sales_Rep__c')).put((String)result.get('Month__c'),(Decimal)result.get('expr0'));
        }
    }
}

And your page would need to build upon this:

<apex:page controller="SalesRevenueController">
  <apex:repeat value="{!salesRevenueDetails}" var="rep">
     <b>Sales Rep: {!rep}</b>
     <br />
     <apex:repeat value="{!salesRevenueDetails[rep]}" var="month">
         Month: {!month}
         <br />
         Total Revenue: {!salesRevenueDetails[rep][month]}
         <br/>
     </apex:repeat>
  </apex:repeat>
</apex:page>

The controller queries the object and uses aggregate functions with grouping to provide a series of aggregate result objects that we can loop through and place into a map for use later on (I use a map here because it is a standard primitive. As mentioned above a wrapper class may have been a better idea but is not used here to make the code easier for you to use).

Using this map we set up a pair of apex:repeat tags which loop through the map to display the items as needed. This setup can be easily styled as required to provide a better look and feel for your users. You will need to add testing for this to deploy it to a production environment.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .