Record is read-only : Trying to update in after insert trigger

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).

In this instance you’ll need to clone the record (or query anew via SOQL) and make changes to the new copy of the record, then execute the update against the new copies

Permanent link to this article: https://salesforcebuddy.com/2019/01/record-is-read-only-trying-to-update-in-after-insert-trigger/

1 comments

    • Andy on January 25, 2019 at 5:09 pm
    • Reply

    This Is helpful, I was facing a similar issue. Good Job

Leave a Reply

Your email address will not be published.