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
Nick WhiteNick White 

System.FinalException: Record is read-onlyTrigger

I have the following basic Trigger.  This works well when opening a new or amending an existing record and saving.

trigger QuoteUpdateGPN2 on Quotation_Detail__c (before update) {
    for (Quotation_Detail__c a : Trigger.new) {
        if (a.GPN_Link__c == null) {
            a.GPN_Link__c = [Select Id FROM Product2 Where Name =:a.Part_Number__c][0].Id;
        }
    }
}

Unfortunately it is not working as part of an "after insert" trigger, for when I upload data via Dataloader, as it indicated the field is Read Only.
I understand why, it is indicating as read only, as the field is active, but I am unable to solve.

How can I make this trigger work with "after insert", as well as "before update"

Many thanks
Best Answer chosen by Nick White
Samir Shaikh7Samir Shaikh7
Hi Nick, you can update the current context record in before update but if do it in after insert or after update event then you will get System.FinalException. Instead of after insert you can do the same in before insert event and you will not get the System.Final Exception

All Answers

Samir Shaikh7Samir Shaikh7
Hi Nick, you can update the current context record in before update but if do it in after insert or after update event then you will get System.FinalException. Instead of after insert you can do the same in before insert event and you will not get the System.Final Exception
This was selected as the best answer
Nick WhiteNick White
Hi Samir, this worked.  So easy when you know how. :-)  Thanks very much...