+ Start a Discussion

Trigger to calculate OppLineItem's value - implementation question

I am looking for some design ideas on a project I am working on.



On the opportunity, when a specific part is added, we want to autocalculate the value/price of that part.  In our case, the part is maintenance/support, which is 18% of all software line items on that opportunity.  So whenever any line item changes, we want to update that part's price if it exists.


What is the best way of implementing this, both with triggers in mind and Govenor Limits?


Situations to consider:

  • Line item that is changed could be the maintenance/support line item itself or another line.  This could make it tricky to handle via a before update/before add trigger as I see two paths in code for retrieving and updating the support line item: One as soql and other as Trigger.new.
  • Batch updates:  We will at one point implement a mass renewal process in which we will generate 200+ opportunities at once.  So how would you write the trigger to accomidate this with regard to Govenor Limits?








If I understand the use-case here's some thoughtful pseudocode:



//first get a list of opptys affected


for(opportunitylineitem o : trigger.new){





//then find the total value without maintenance/support of those opptys


for(opportunitylineitem o : [select * from opportunitylineitem where opportunityid in : somelist and type != main/supp]){  if(somemap.containskey(o.opportunityid)){

      totalval = somemap.get(o.opportunityid) + o.value;









//then assign the value you want for maintenance/support and update


for(opportunitylineitem o : [select * from opportunitylineitem where opportunityid in : somelist and type = main/supp]){  OpportunityLineItem newo = new OpportunityLineItem(id = o.id, value = 0.18*somemap.get(o.opportunityid));



update listtoupdate;




watch out for updating the same records that came through the trigger.


another thought is to use salesforce's rollup summary fields to store the current maintenance/support value and the current non-maintenance/support value on the oppty record itself.  this feature is a little buggy, so watch out, but it might be a little cleaner solution.