An effort to bring a revolution in Salesforce.

Friday 25 September 2015

Salesforce !! which record is read-only trigger?

Which record is read-only trigger Salesforce.

"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"

Examples:

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

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 ='abc';
  }
}

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 ='abc';
  }
}
trigger mytrigger on account(before insert,before update){
  for(account ac:trigger.old){
      ac.name ='abc';
  }
}

Notes:
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"
,

No comments:

Post a Comment

Get Gmail, Docs, Drive, and Calendar for business