+ Start a Discussion
Bhart KumarBhart Kumar 

why records fetched inside after triggers or via trigger.old are read only? Can anyone please explain with an example?

AbhinavAbhinav (Salesforce Developers) 
Hi Bhart,

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!
Suraj Tripathi 47Suraj Tripathi 47
Hi Bhart,

 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

 
Manj_SFDCManj_SFDC
The records are written but they are not committed into the database hence they will be read only, you can perform the DML post commit to Database