Saturday, July 1, 2017

what is 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';
  }
}
Note:
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"
Even if we wand to save the record in read only mode trigger see the below code:
trigger ReadonlyforconTrigger on Contact (after insert) {
    
    if(trigger.isafter && Trigger.isinsert){
        for(contact con : trigger.new){
        if(con.department== Null ){
            ReadonlyforconCls.m1(con.id);
       //  con.department = 'readonly';
        }
        }
    }
}
------------
public class ReadonlyforconCls {
    public static void m1(string conid){
        contact con =[select id,lastname,department,PickVal__c from contact where id =:conid];
        con.department = 'readonly';
        update con;
    }

}

No comments:

Post a Comment