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

Before Trigger Seems to Prevent after Trigger on OpportunityLineItem - Confused and Need Help, Please
On the OpportunityLineItem I have two triggers: One that updates the Unit Price and another that applies a Discount before the record is Saved and another that Updates a field on the Opportunity. Both work fine separately but when put together the insert after creates a problem where I get the following error message:
OpportunityLineItemTrigger: execution of AfterInsert caused by: System.FinalException: Record is read-only Trigger.OpportunityLineItemTrigger: line 12, column 1
Your help would be greatly appreciated.
Robert
OpportunityLineItemTrigger: execution of AfterInsert caused by: System.FinalException: Record is read-only Trigger.OpportunityLineItemTrigger: line 12, column 1
trigger OpportunityLineItemTrigger on OpportunityLineItem (before insert, before update, after update, after insert) { List<Opportunity> opps = new List<Opportunity>(); List<Id> oppIds = new List<Id>(); Decimal contractLengthMonths = 0; //---------------BEFORE TRIGGER BELOW---------- if(trigger.isInsert || trigger.isUpdate && trigger.isBefore) { List<OpportunityLineItem> oli = new List<OpportunityLineItem>(); for (OpportunityLineItem oli2: trigger.new){ if(oli2.Number_of_Months__c > 0){ oli2.UnitPrice = ((oli2.Number_of_Months__c / 12) * oli2.Unit_Price2__c); } else if (oli2.Number_of_Months__c == null || oli2.Number_of_Months__c == 0 ) { oli2.UnitPrice = oli2.Unit_Price2__c; } if(oli2.Adjusted_Gross_Margin_Percentage__c == 0 && oli2.Product_Gross_Margin_Percent2__c != 0 ) { if (oli2.Number_of_Months__c > 0) { oli2.Gross_Margin_Dollars__c = (oli2.Unit_Price2__c * oli2.Quantity * (oli2.Number_of_Months__c / 12)) * (oli2.Product_Gross_Margin_Percent2__c/100); } else { oli2.Gross_Margin_Dollars__c = (oli2.Unit_Price2__c * oli2.Quantity) * (oli2.Product_Gross_Margin_Percent2__c/100); } } else if (oli2.Adjusted_Gross_Margin_Percentage__c > 0) { if (oli2.Number_of_Months__c > 0) { oli2.Gross_Margin_Dollars__c = (oli2.Unit_Price2__c * oli2.Quantity * (oli2.Number_of_Months__c / 12)) * (oli2.Adjusted_Gross_Margin_Percentage__c/100); } else { oli2.Gross_Margin_Dollars__c = (oli2.Unit_Price2__c * oli2.Quantity) * (oli2.Adjusted_Gross_Margin_Percentage__c/100); } }//END if(oli2.Adjusted_Gross_Margin_Percentage__c == 0 || oli2.Adjusted_Gross_Margin_Percentage__c == null) }//END for (OpportunityLineItem oli2: trigger.new) }//if(trigger.isInsert || trigger.isUpdate && trigger.isBefore) //---------AFTER TRIGGER BELOW-------------- if(trigger.isAfter) { for (OpportunityLineItem oli3: trigger.new){ oppids.add(oli3.opportunityId); }//END for (OpportunityLineItem oli3: trigger.new) List<OpportunityLineItem> allOLI = [SELECT id, Number_of_Months__c FROM OpportunityLineItem WHERE OpportunityId in: oppids]; List<Opportunity> oppsToUpdate = [SELECT id, Contract_Length_Months__c FROM Opportunity WHERE id in: oppids]; if(allOLI.size() > 0){ for(OpportunityLineItem allOLI2: allOLI){ if(allOLI2.Number_of_Months__c > contractLengthMonths){ contractLengthMonths = allOLI2.Number_of_Months__c; } else if (allOLI2.Number_of_Months__c == null) { contractLengthMonths = 12; } } //END for(OpportunityLineItem allOLI2: allOLI) for(Opportunity oppUpdate: oppsToUpdate){ oppUpdate.Contract_Length_Months__c = contractLengthMonths; opps.add(oppUpdate); }// END for(Opportunity oppUpdate: oppsToUpdate) }//END if(allOLI.size() > 0 update opps; }//if(trigger.isAfter) }//END OpportunityLineItem Class
Your help would be greatly appreciated.
Robert
Please use below line at Line no 7 instead of if(trigger.isInsert || trigger.isUpdate && trigger.isBefore) {
Hope it helps you.
Thanks
Varaprasad
All Answers
Please use below line at Line no 7 instead of if(trigger.isInsert || trigger.isUpdate && trigger.isBefore) {
Hope it helps you.
Thanks
Varaprasad
THANK YOU. Works like a charm. You saved me. Thanks.
Robert