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
EnryEnry 

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.

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

Did you do something like this ?

 And am positive if everything else is corrent the code should work

for(Opportunity newOppt : Trigger.new){

           if(trigger.isBefore){
newOppt.NextStep='NextStep';
} opptsToInsert.add(newOppt); }

All Answers

Avidev9Avidev9

Well I guess you can add a filter for "before" event

 

trigger Automations on Opportunity (before insert,after insert) {
 List<Opportunity> opptsToInsert = new List<Opportunity>();

     for(Opportunity newOppt : Trigger.new){

           if(trigger.isBefore)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);
          }  
      }    

}
EnryEnry

Thank you very much Avi, It works!

Avidev9Avidev9

Did you do something like this ?

 And am positive if everything else is corrent the code should work

for(Opportunity newOppt : Trigger.new){

           if(trigger.isBefore){
newOppt.NextStep='NextStep';
} opptsToInsert.add(newOppt); }
This was selected as the best answer