You need to sign in to do that
Don't have an account?
Enry
System.FinalException: Record is read-only
Hi,I want just that when a new opportunity is created, a set of opportunitylineitems will be created.
This is my code:
trigger Automations on Opportunity (before insert,after insert) { List<Opportunity> opptsToInsert = new List<Opportunity>(); for(Opportunity newOppt : Trigger.new){ newOppt.NextStep='NextStep'; opptsToInsert.add(newOppt); } //CREATE 1 OPPORTUNITY LINE ITEM FOR EACH NEW OPPORTUNITY WITH THE CORRECT PRODUCT List<OpportunityLineItem> opptLineItemsToInsert = new List<OpportunityLineItem>(); if ((Trigger.isAfter)&&(Trigger.isInsert)){ for(Opportunity opptInserted : [Select Id, Description from Opportunity where Id in :opptsToInsert]) { for (Integer i=0;i<4; i++) { OpportunityLineItem newOpptLineItem = new OpportunityLineItem(); newOpptLineItem.OpportunityId=opptInserted.Id; newOpptLineItem.Quantity = 1; // SET PRODUCT newOpptLineItem.PricebookEntryId ='01ui0000002brJzAAI'; newOpptLineItem.TotalPrice=100; opptLineItemsToInsert.add(newOpptLineItem); } insert(opptLineItemsToInsert); } } }
i get the following error:
Automations: execution of AfterInsert caused by: System.FinalException: Record is read-only
it works if a remove newOppt.NextStep='NextStep';
The problem is that you cannot update fields on triggered records when you are in an after trigger but i have to update information for the new opportunity.
Please can you help me with this?
Thank you very much for any advice.
ps:I know that it very bad use hard coded ids,but this is just a test.
Br.
Did you do something like this ?
And am positive if everything else is corrent the code should work
All Answers
Well I guess you can add a filter for "before" event
Thank you very much Avi, It works!
Did you do something like this ?
And am positive if everything else is corrent the code should work