Skip to main content
Hi,

I'm having some difficulties understanding how to work properly with aggregate result.   I have read several resources like https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_agg_fns.htm

But still am not getting it.

A little background, what I would like to do is sum product quantities (opportunity line items).  And sum them by tier, and then by tier and variety.  So I imagine the output would be something like

Tier 1 total = 5

  Cab Sauv = 3

  Merlot = 2

Tier 2 total = 3

Chardonnay = 3

etc

I tried a real basic query like so.  The opportunity has three products each with different tiers.  If they have the same tier, I get the sum of the quantities, but I can't seem to figure out how to get the quantities of the other tiers.

 

List<AggregateResult> TierData;

TierData = [SELECT sum (Quantity) TotalQuantity, Tier__c

FROM OpportunityLineItem WHERE OpportunityID = '006W0000005llrP' GROUP BY Tier__c LIMIT 1];

system.debug('TierData = ' +TierData);

for (AggregateResult agg:TierData) {

system.debug ('agg = ' + agg);

}

Same problem if I try GROUP BY Tier__c, variety__c

How do I get the data out properly.  

Thanks,
2 Antworten
  1. 12. Aug. 2015, 23:07
    So I found the error,  and now I feel REAL stupid.  I have a LIMIT 1 clause in my SOQL query.  Not sure why I put it in, but there you go.  

    Thanks,
0/9000