0

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.

VFPage:

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

<apex:form >
<html>
<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>
<td>Quantity</td>
</tr>
<apex:outputLabel ></apex:outputLabel>
<apex:repeat value="{!wrapList}" var="wrap">
<tr bgcolor="orange">
<td>{!wrap.type}</td>
<td></td>
<td></td>
<td></td>
<Td></td>
<td></td>
<td></td>
</tr>
<apex:repeat value="{!wrap.opptyLineItemList}" var="linteItem">

<Tr>
<td>{!linteItem.Opportunity.account.name}</td>
<td>{!linteItem.Opportunity.Name}</td>
<td>{!linteItem.id}</td>
<td>{!linteItem.ProductCode}</td>
<td>{!linteItem.Name}</td>
<td>{!linteItem.TotalPrice}</td>
<td>{!linteItem.Quantity}</td>
</tr>
</apex:repeat>
<tr bgcolor="gray">
<td></td>
<td></td>
<td></td>
<td></td>
<Td>Total</td>
<td>{!calcMap[wrap.type].total}</td>
<td>{!calcMap[wrap.type].quantity}</td>
</tr>

</apex:repeat>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<Td>Grand Total</td>
<td>{!GrandTotal}</td>
<td>{!GrandQuantity}</td>
</tr>
</table>
</html>
</apex:form>
</apex:page>

Controller:

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()
  { 
    generateLogic();
  }

 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)
{

    if(opptyLineItemMap.containsKey(opptyline.Opportunity.StageName))
    { 
       List<OpportunityLineItem> opptyLineItemLists = new List<OpportunityLineItem>();

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

       if(opptyLineItemLists!=null)
       opptyLineItemLists.add(opptyline);

       opptyLineItemMap.put(opptyline.Opportunity.StageName,opptyLineItemLists);

      OpptyLineWrapper wrapVal = new OpptyLineWrapper();

       if(calcMap.containsKey(opptyline.Opportunity.StageName))
       {
           wrapVal = calcMap.get(opptyline.Opportunity.StageName);
           System.debug('******wrapVal*******'+wrapVal);

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

       }

    }
    else
    {
       OpptyLineWrapper wrap = new OpptyLineWrapper();

        wrap.OpptyId = opptyline.OpportunityId;

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

        wrap.opptyLineItemList.add(opptyline);

        opptyLineItemMap.put(opptyline.Opportunity.StageName,wrap.opptyLineItemList);

        wrap.total = opptyline.TotalPrice;
        wrap.quantity = opptyline.Quantity;
        wrap.type = opptyline.Opportunity.stagename;
        calcMap.put(opptyline.Opportunity.StageName,wrap);
        wrapList.add(wrap);
}
    }


  GrandTotal = 0.0;
  GrandQuantity=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()
    {
      total=0.0;
      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!!

0

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.