• deepshikha shriwas 20
  • NEWBIE
  • 5 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 5
    Replies
I have a requirement to count the number of opportunity line items and populate it to the account.
I was thinking to use Aggregate queries,but is it poosible to do it using this.
I am in process of creating this however i am stuck:

public with sharing class opportunityTriggerHandlerClass {
    public static void countofOliItemsMethod(List<OpportunityLineItem> oliList){
        Set<Id> oppIdSet = new Set<Id>();
        Set<Id> accIdSet = new Set<Id>();
        for(OpportunityLineItem oli: oliList){
            oppIdSet.add(oli.OpportunityId);
        }
        for(Opportunity oppty : [SELECT ID,AccountId FROM Opportunity WHERE Id IN: oppIdSet]){
             accIdSet.add(oppty.AccountId) ;
        }
        List<AggregateResult> aggr = [SELECT OpportunityId,Opportunity.AccountId,Opportunity.Account.Name,count(Id)oli FROM               

                                                          OpportunityLineItem 
                                                          WHERE Opportunity.AccountId IN:accIdSet GROUP BY Opportunity.AccountId] ;
    }
}

I face an error that "Field must be grouped or aggregated: OpportunityId".

Can anybody tell me how should we approach here.

Thanks in advance!