record is read-only trigger

This post is regarding a error that we get because of trigger. "execution of AfterUpdate caused by: System.FinalException: Record is read-only"
This kind of error occurs if you try to update lists/record which are/is read only in the trigger execution. For example, trigger.new and trigger.old are both read only lists and cannot be applied with a DML operation.
Say if you write Update trigger.new; in your trigger you will get the above mentioned error.
A field value of object can be changed using trigger.new but only in case of before triggers. But in case of after triggers changing the values of the records in trigger.new context will throw exception as "Record is read only"
Example:

trigger mytrigger on account(before insert,before update){
  for(account ac:trigger.new){
      ac.name ='new name';
  }
}
Above code will update the names of the records present in trigger.new list. But, the below code will throw run time exception "Record is read only".
trigger mytrigger on account(after insert,after update){
  for(account ac:trigger.new){
      ac.name ='new name';
  }
}
Also trigger.old will always be read only no matter where you use it either before trigger or after trigger.
That is both the below codes will throw run time exception as trigger.old is read only
trigger mytrigger on account(after insert,after update){
  for(account ac:trigger.old){
      ac.name ='new name';
  }
}
trigger mytrigger on account(before insert,before update){
  for(account ac:trigger.old){
      ac.name ='new name';
  }
}
Let us briefly summarise on what we discussed above:
1. Trigger.new and trigger.old are read only
2. An object can change its own field values only in before trigger: trigger.new
3. In all cases other than mentioned in point 2; fields values cannot be changed in trigger.new and would cause run time exception "record is read only"

3 comments:

  1. Very nice site about trigger in SFDC like when it use before or after like that.
    It is very useful for the SFDC developers.

    Thank for CloudeForce4u Team

    ReplyDelete
  2. This is nice and valuable input

    ReplyDelete
  3. Hello,

    I have a doubt here. Can you please clarify.

    trigger mytrigger on account(before insert,before update){
    for(account ac:trigger.old){
    ac.name ='new name';
    }
    }

    -- when this code executed, getting error System.NullPointerException: Attempt to de-reference a null object.
    It means that there were no records at trigger.old. So how can we say trigger.old is read-only at before triggers?

    ReplyDelete