Find your content:

Search form

You are here

Aggregating Results: Not grouping properly

 
Share

I have a query onto the OpportunityLineItem table.

AggregateResult[] OppLineItemsPlays = [select OpportunityId, PricebookEntry.Product2.Play__c, COUNT(id) 
                                            from OpportunityLineItem 
                                            where OpportunityId in: oppid
                                            group by OpportunityId, PricebookEntry.Product2.Play__c];
        System.debug('\n ------------------->OppLineItemsPlays = ' + OppLineItemsPlays);

I expected results like:

Count=1, OpportunityId=1, Play__c=RED}, 
Count=2, OpportunityId=1, Play__c=WHITE}, 
Count=1, OpportunityId=2, Play__c=WHITE},
Count=2, OpportunityId=2, Play__c=BLACK}  
Count=2, OpportunityId=3, Play__c=RED},  
Count=2, OpportunityId=4, Play__c=RED}, 
Count=2, OpportunityId=4, Play__c=BLACK}

So that I get the number of OLIs that have the same Playcode for each opportunity

What I get is:

AggregateResult:{expr0=3, OpportunityId=1, Play__c=RED, 
AggregateResult:{expr0=2, OpportunityId=2, Play__c=WHITE}, 
AggregateResult:{expr0=2, OpportunityId=4, Play__c=BLACK}

I was thinking that the GROUP BY would give me the equivalent of a dual Primary key... I want to know how many OLIs for OPP1 have Play = RED, then push that to the Opportunity. (I know a rollup would work - but we've run out and there are no more to be had)

These are batch loaded / updated so the current code works ok when working with a single opportunity no matter how many Products are added. When >1 opportunity is added the Aggregate fails


Attribution to: SeanGorman
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/31025

My Block Status

My Block Content