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

Opportunity Trigger Does Not Fire

The intent of the trigger below is to update two opportunitylineitem data elements after the opportunity (after update) trigger fires. The trigger is fired using the same opportunitylineitem that is suppose to be updated by the trigger. 
The trigger does not fire when I insert a opportunitylineitem but it does when I delete an opportunitylineitem. Any ideas why it would not trigger on insert?
Can you not update the record that fired a trigger in an after update/after insert opportunity trigger?


trigger ChannelCommissioningSalesPrieceCalculation on Opportunity (after update) { //a list of unique Product from above List<Product2> lProduct2 = [Select p.Id, p.Channel_Commission__c From Product2 p]; Map<string, Boolean> mProduct2= new Map<string, Boolean>(); List<Opportunity> lOpportunity =[Select o.Id, (Select Id, Channel_Commissioning__c, Cost__c, TotalPrice, UnitPrice From OpportunityLineItems) from Opportunity o where o.Id =:[0].Id ]; List<OpportunityLineItem> lOpportunityLineItem = lOpportunity[0].OpportunityLineItems; Decimal dTotal=0; Decimal dChannel_Commissioning =0; String aId = '00k8000000BpaHc'; for (Integer i=0; i<lOpportunityLineItem.size(); i++) { if (true== mProduct2.get(lOpportunityLineItem[i].Id)) { aId = lOpportunityLineItem[i].Id; dChannel_Commissioning = lOpportunityLineItem[i].Channel_Commissioning__c; } else { dTotal += lOpportunityLineItem[i].TotalPrice; } } System.Debug('---------------------------------'); System.Debug(dTotal); System.Debug(dChannel_Commissioning); System.Debug(aId); System.Debug(lOpportunityLineItem.size()); System.Debug('---------------------------------'); if (0 != dChannel_Commissioning) { //OpportunityLineItem is updated with UnitPrice and Cost based on calculation. OpportunityLineItem aOpportunityLineItem = new OpportunityLineItem(Id= aId); aOpportunityLineItem.UnitPrice = 100*(dTotal/(dChannel_Commissioning )); aOpportunityLineItem.Cost__c= 100*(dTotal/(dChannel_Commissioning )); update aOpportunityLineItem; } }



not sure if I understand your code correctly. Here are some thoughts:


1. the Map mProduct2 is never populated, that means your condition if(true ==

is never met


2.  All you do is related to the line items, so why don't you write the trigger on the line items instead of the opportunity?


3. If you for some reason need to write the trigger on opportunity, have uou activated the new opportunity save behavior in the critical updates?


4. Have you tried to this with a formula field on line items? I am not absolutely sure but think it should be possible


Hope that helps

Message Edited by ahab1372 on 06-03-2009 03:47 PM