I'm writing a trigger for new opportunity products to populate a field Product_Category__c (on opportunity lineitem) getting the value from Product_Category_new__c (on Product).
This is my trigger:
trigger SetProductCategory on OpportunityLineItem (after insert) {
for (OpportunityLineItem opplineitem: Trigger.new) {
opplineitem.Product_Category__c = opplineitem.PricebookEntry.Product2.Product_Category_new__c;
}
}
I get this error message:
execution of AfterInsert caused by: System.FinalException: Record is read-only
I have tried with before insert;in this case there aren't errors but the value Product category it's not saved on Opportunity Line item.
I have found that:
In a after insert trigger is not allowed change field using trigger.new
Please can you suggest a solution for this? i would like to copy the value Product category (Product) on Product Category (Opportunity Product) after the insert of the new opportunity products.
Thanks in advantage for any advice.
Br