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
pooja kesharwanipooja kesharwani 

Trigger2: execution of AfterInsert caused by: System.FinalException: Record is read-only Trigger.Trigger2: line 5, column 1

Hi,

I am trying to update an Account record after insert. Below is my code:
 
trigger Trigger2 on Account (after insert, after update) {

    List<Account> acc= new List<Account>();
    for(Account a:trigger.new){
        a.Industry='Chemicals'; 
        acc.add(a);
}
    update acc;
}

When I am trying to save the record, it is giving me the error:

Trigger2: execution of AfterInsert caused by: System.FinalException: Record is read-only Trigger.Trigger2: line 5, column 1
​​​​​​​
Best Answer chosen by pooja kesharwani
PriyaPriya (Salesforce Developers) 
Hi Pooja,

Refer this similar scenario :- 
https://developer.salesforce.com/forums/?id=9062I000000IJtxQAG

You cannot update the record fields in an after insert trigger because the data is already updated in the database. It's simply prohibited. The only way to update anything in an after insert context is to issue new DML statements (insert/update/upsert/delete), but never do this to the records that the trigger is processing -- you're likely to create an infinite loop of updates.

In my experience, after insert triggers should only update records in objects the trigger is not processing. For example, to replicate something from an account to an opportunity, update the opportunity records in an after insert trigger on the account. But pulling data into an opportunity from the account would be a before insert trigger on the opportunity that would query the related accounts (in a single, bulkified query).
 

Kindly mark it as the best answer if it works for you.

 

Thanks & Regards,

Priya Ranjan