function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ty WhitfieldTy Whitfield 

PageBlockTable with Group of Items

So I have a pageblocktable that I populate with 
Public List<OpportunityLineItem> oppLineItem {get;set;}
 oppLineItem = [Select Id, Discount, ListPrice, TotalPrice,  PricebookEntryId, Product2Id, ProductCode, Name, Quantity,  UnitPrice, Description FROM OpportunityLineItem WHERE OpportunityId = :oppId];
What I would like to have happen is to group by Name and generate a sum for the Quantity. 

Is that possible or is there a better option than pageblocktable.  Note, I was only using the pageblocktable as I am not sure of how many items will be returned with the query.
 
Best Answer chosen by Ty Whitfield
Prashant Pandey07Prashant Pandey07
Hi,

If you need to get the Aggregate result then the best option is to query the record using AggregateResult.

list<AggregateResult> groupedResults
  = [Select Id, Discount, ListPrice, TotalPrice,  PricebookEntryId, Product2Id, ProductCode, Name, Sum(Quantity),           UnitPrice, Description 
      FROM OpportunityLineItem WHERE OpportunityId = :oppId and GROUP BY Name];

--
Thanks,
Prashant

All Answers

Prashant Pandey07Prashant Pandey07
Hi,

If you need to get the Aggregate result then the best option is to query the record using AggregateResult.

list<AggregateResult> groupedResults
  = [Select Id, Discount, ListPrice, TotalPrice,  PricebookEntryId, Product2Id, ProductCode, Name, Sum(Quantity),           UnitPrice, Description 
      FROM OpportunityLineItem WHERE OpportunityId = :oppId and GROUP BY Name];

--
Thanks,
Prashant
This was selected as the best answer
Ty WhitfieldTy Whitfield
Thanks, that got me started. I wasn't able to group by name, it kept giving an error but  I was able to create an aggregateresult and then create a new list to bring everything in together.
Prashant Pandey07Prashant Pandey07
Glad you are able to proceed further..
Please select the best answer and close this thread.

--
Thanks,
Prashant