You need to sign in to do that
Don't have an account?

Update Opportunity from LineItem trigger
I am working on a trigger that will sum some of the values from the opportunityLineItems and update a field on the opportunity. These are custom fields that do not automatically sum and our org has already used 100% of available roll-up summary fields and there are existing integration pieces that rely on these fields.
Currently they are updated via s-control that has to be manually envoked from a link on each record.
I have some experience with Apex Trigger, but am hoping somebody may be able to help me get started...
So far I have:
trigger Calculate_Products on OpportunityLineItem (after delete, after insert, after undelete, after update) { //Set the OpportunityID Set<id> Ids = new Set<id>(); for (OpportunityLineItem oli : Trigger.new) { Ids.add(oli.OpportunityId); } //Query the OpportunityLineItems List<aggregateResult> results = [select sum(margin__c)m from opportunityLineItem where OpportunityId in :Ids]; for (OpportunityLineItem oli : Trigger.new) { Opportunity Opp = opptys.get(oli.OpportunityId); Opp.Margin_Total__c = results.get('m'); } update opp; }
but I am not quite sure how to I can sum the margin__c field and update the opportunity total_margin__c field... or how to ensure the trigger is bulk friendly.
Thanks in advance for any help!
