If you have encountered this error while working on Triggers. This is because once you are in an after insert/update trigger and the records are read only in that context as they have been written, but not committed, to the database. Unfortunately your trigger is relying on the ids of the records, which means you won’t be able to use before insert as the ids won’t be populated at that time (as the records haven’t been written to the database at that point so while you can write to them, database generated fields aren’t populated).
This kind of error occurs if you try to update lists/record which are/is read-only in the trigger execution. Please refer below link it may be helpfull for you.
If you have encountered this error while working on Triggers. This is because once you are in an after insert/update trigger and the records are read only in that context as they have been written, but not committed, to the database.
Unfortunately your trigger is relying on the ids of the records, which means you won’t be able to use before insert as the ids won’t be populated at that time (as the records haven’t been written to the database at that point so while you can write to them, database generated fields aren’t populated).
reference:
http://salesforcebuddy.com/2019/01/record-is-read-only-trying-to-update-in-after-insert-trigger/
check below to link to get clarity on Trigger.old and Trigger.new
https://help.salesforce.com/s/articleView?id=000323125&type=1
If it helps mark it as best answer.
Thanks!
This kind of error occurs if you try to update lists/record which are/is read-only in the trigger execution.
Please refer below link it may be helpfull for you.
http://sfdcsrini.blogspot.com/2014/09/what-is-record-is-read-only-trigger.html
If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi