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

How can I subtotal Opps in Visualforce template?

I need to provide subtotals in a VF template e.g.



  Setup £500 


  Hosting 12 x £10 

  Software Lice 12 x £10


Total One off costs: £500

Total Ongoing Costs: £20 per month 


I have managed to break down the line items on the opp by using a custom field on the product to designate its an annual license field


however, my totals are for the total annual amount e.g. £740 in the above example


<tr> <td class="section" colspan="4">INITIAL COSTS</td> </tr> <apex:repeat var="opp" value="{!relatedTo.OpportunityLineItems}"> <apex:outputText rendered="{!opp.PriceBookEntry.Product2.Annual_Cost__c == false}"> <tr> <td>{!opp.PriceBookEntry.Name}<br>{!opp.PriceBookEntry.Product2.Description}<br>{!opp.Description}</td> <td>{!ROUND(opp.Quantity,1)}</td> <td align="right">£{!ROUND(opp.UnitPrice*1.00,2)}</td> <td align="right">£{!ROUND(opp.TotalPrice*1.00,2)}</td> </tr> </apex:outputText> </apex:repeat> <tr> <td class="section" colspan="4">ONGOING COSTS</td> </tr> <apex:repeat var="opp2" value="{!relatedTo.OpportunityLineItems}"> <apex:outputText rendered="{!opp2.PriceBookEntry.Product2.Annual_Cost__c == true}"> <tr> <td>{!opp2.PriceBookEntry.Name}<br />{!opp2.PriceBookEntry.Product2.Description}<br />{!opp2.Description}</td> <td>{!ROUND(opp2.Quantity,1)}</td> <td align="right">£{!ROUND(opp2.UnitPrice*1.00,2)}</td> <td align="right">£{!ROUND(opp2.TotalPrice*1.00,2)}</td> </tr> </apex:outputText> </apex:repeat> <tr> <td class="section" align="right" colspan="3">TOTAL NET</td> <td align="right">£{!ROUND(relatedTo.Amount * 1.00, 2)}</td> </tr> <tr> <td class="section" align="right" colspan="3">TOTAL TAX</td> <td align="right">£{!ROUND(((relatedTo.Amount * 1.15) - relatedTo.Amount), 2)}</td> </tr> <tr> <td class="section" align="right" colspan="3">GROSS AMOUNT</td> <td align="right">£{!ROUND(relatedTo.Amount * 1.15, 2)}</td> 



Hi Adam,


your problem description doesn't have quite enough detail to determine the problem, but I think what are saying is that you have 2 types of opportunity products: those with a fixed cost and those with an annual cost. If I understand correctly, you want those toroll up to the opportunity as 2 separate totals.


If that understanding is correct, then you can do exactly what you are describing by creating 2 master-detail rollups field on Opportunity. They both will be SUM fields on opp product, but will each be filtered, such that one only sums monthly products and the other only sums fixed cost products.


If my understanding is not correct, can you provide some more details on your schema and the exact problem you are seeing?


Jeremy Kraybill

Austin, TX


Yes that is what I am looking to do.


I am using standard Opportunity and Opp Item lines - I'm guessting I would need to add a new Custom Object - "Recurring Costs" which uses some sort of rollup on the Opportunity


I'm guessing that would also make our renewals easier to process.


This one has had me pulling my hair out - so much so we even looked at SugarCRM to see how it handled this!! 


No, not a new custom object, just 2 custom fields on opportunity, each of which is a master/detail rollup.


This is very standard functionality in salesforce. The main limitation of M/D rollups is that you can only use them on Master/Detail relationships (e.g. Opportunity --> Opportunity Line Item). No offense intended here, but if this use case had you pulling out your hair, I really recommend you go through some of the free online Salesforce training sessions -- they're very useful and well put together.


Jeremy Kraybill

Austin, TX


Right -- got it - Only one slight problem in that the "Annual Cost" flag I added onto the product needs to be copied to the  Opportunity Item Line each time I add a product - or I could use revenue schedules but thats a whole new area for us to look at.


Thanks for your help