I am trying to display a table in visualforce page. The table contains list of Opportunity Lines and totals Account —> 4 Opportunities (with Oppty Type as New Business and Add-On).
First Oppty —> 3 Opportunity lines
Second Oppty —> 2 Opportunity lines.
Third Oppty —> 3 Opportunity Lines.
Fourth Oppty —> 2 Opportunity lines.

I want to group the lines depending on the type and display the total for each OpptyName and display the GrandTotal for the complete table.
I am able to group it with Oppty type and get the complete totals for each type but not able to get the individual oppty totals.
Below is the vfpage and controller written and output.


<apex:page controller="TestController2" sidebar="false">

<apex:form >
<table border="5" align="center">
<tr bgcolor="yellow">
<td>Account Name</td>
<td>Opportunity Name</td>
<td> Record Id </td>
<td>Product Code</td>
<td>Product Name</td>
<td>Product Price</td>
<apex:outputLabel ></apex:outputLabel>
<apex:repeat value="{!wrapList}" var="wrap">
<tr bgcolor="orange">
<apex:repeat value="{!wrap.opptyLineItemList}" var="linteItem">

<tr bgcolor="gray">

<Td>Grand Total</td>


global class TestController2
  public List<OpptyLineWrapper> wrapList {set;get;}
  public Map<String,OpptyLineWrapper> calcMap {set;get;}
  public Decimal GrandQuantity {set;get;}
  public Decimal GrandTotal {set;get;}

  public TestController2()

 public void generateLogic()
 wrapList = new List<OpptyLineWrapper>();
 calcMap = new Map<String,OpptyLineWrapper>();

 Map<String,List<OpportunityLineItem>> opptyLineItemMap = new Map<String,List<OpportunityLineItem>>();

List<OpportunityLineItem> opptyLineItemList = [SELECT Id,Name,OpportunityId,Opportunity.name,Opportunity.account.name,Opportunity.StageName,ProductCode,Quantity,TotalPrice FROM OpportunityLineItem];
System.debug('**********ooptyLineItemList ******'+opptyLineItemList.size());
if(opptyLineItemList!=null && opptyLineItemList.size()>0)
for(OpportunityLineItem opptyline : opptyLineItemList)

       List<OpportunityLineItem> opptyLineItemLists = new List<OpportunityLineItem>();

       opptyLineItemLists = opptyLineItemMap.get(opptyline.Opportunity.StageName);



      OpptyLineWrapper wrapVal = new OpptyLineWrapper();

           wrapVal = calcMap.get(opptyline.Opportunity.StageName);

           if(wrapval!=null && opptyline.totalprice!=null)
           wrapVal.total = wrapval.total + opptyline.totalprice;
           if(wrapval!=null && opptyline.quantity!=null)
           wrapVal.quantity = wrapval.quantity + opptyline.quantity;


       OpptyLineWrapper wrap = new OpptyLineWrapper();

        wrap.OpptyId = opptyline.OpportunityId;

        wrap.opptyLineItemList = new List<OpportunityLineItem>();



        wrap.total = opptyline.TotalPrice;
        wrap.quantity = opptyline.Quantity;
        wrap.type = opptyline.Opportunity.stagename;

  GrandTotal = 0.0;
  for(OpptyLineWrapper wrap : calcMap.values())
      GrandTotal = Grandtotal + wrap.total;
      GrandQuantity = GrandQuantity + wrap.quantity;

 public class OpptyLineWrapper
    public Id OpptyId {set;get;}
    public List<OpportunityLineItem> opptyLineItemList {set;get;}
    public Decimal total {set;get;}
    public Decimal quantity {Set;get;}
    public String type {set;get;}
    public OpptyLineWrapper()
      quantity = 0.0;



Output: OutputFor Above code

Expected Output: Expected Output

Can anyone help me out to get the subtotals for each oppty when grouped by oppty type and what changes I need to make in the code? Appreciate your help in advance!!


This is ideal use case to explore GROUP BY ROLLUP Using GROUPING(fieldName) to Identify Subtotals

This SOQL will be like this:

SELECT  Opportunity.StageName, OpportunityId, ProductCode,
    SUM(Quantity), SUM(TotalPrice),
    GROUPING (Opportunity.StageName) grpOpptyType, GROUPING(OpportunityId) grpOpptyId
FROM OpportunityLineItem
GROUP BY ROLLUP(Opportunity.StageName, OpportunityId,ProductCode)
ORDER BY Opportunity.StageName, OpportunityId

Refer my blog post Efficient coding using Group By Rollup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.