I have a trigger:

   if(Trigger.isAfter && Trigger.isUpdate) {
        for(SomeRecord__c newRecord : Trigger.new){
            if(Trigger.oldMap.get(newRecord.Id).Worker__c != newRecord.Worker__c){
                if(newRecord.Worker__c != NULL){
                    newRecord.WorkersNotes__c = newRecord.Worker__r.WorkersNotes__c;
                } else{
                    newRecord.WorkersNotes__c = '';

When I'm trying to update newRecord.Worker__c, I'm getting System.FinalException: Record is read-only. If I make it before update, nothing will happen. What is wrong here?

P.S. WorkersNotes__c is a Long Text Area, so I can't use the formula field.

And yes: my question and this Exeception on After Insert, Record is read only are not the same. I'm talking about after/before update, not about after insert.

  • you can't update the record in trigger.after context, can you give more explanation about WorkersNotes__c filed update? – sdandamud1 Oct 10 '19 at 15:25
  • It's just a Long Text Area for both objects, and when the child changes it's parent, the new parent gives his WorkersNotes__c to this child. – Apelsinova Oct 10 '19 at 15:38
  • that means when ever Parent SomeRecord__c WorkersNotes__c field updates/add newvalue you need to update/insert same value in to child Cleaner__r WorkersNotes__c field same like vice versa? is it right ? – sdandamud1 Oct 10 '19 at 16:01
  • SomeRecord__c - is a child. The child has just 1 parent. And if it had Worker 1 before and then changed this Lookup Field to Worker 2 - we need to change this field: newRecord.WorkersNotes__c = newRecord.Worker__r.WorkersNotes__c. No Cleaner there, sorry. – Apelsinova Oct 10 '19 at 17:06

You can only update the "new" objects in the before phase. You should typically:

  1. validate and/or update the given objects via the Trigger.new or Trigger.newMap in before phase
  2. update objects related to the Trigger.new or Trigger.newMap objects in after phase

I recommend this trailhead for more info.

| improve this answer | |
  • Thank you. Okay. I make it know with before update. And I'm getting nothing. No changes. When I'm trying to update the record I'm getting DML statement cannot operate on trigger.new or trigger.old. – Apelsinova Oct 10 '19 at 17:08
  • @Apelsinova Have you changed the if condition accordingly? – Ayush Goyal Oct 14 '19 at 11:15
  • If you are using before update phase and are making changes to the Trigger.new objects there is no need to perform an extra DML update operation in the trigger to have these changes committed - I'm guessing you have an update statement in your trigger code, which is what is going wrong. – Phil W Oct 14 '19 at 11:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.