You need to sign in to do that
Don't have an account?
ZG
Unable to update original object in a before delete trigger: "System.FinalException: Record is read-only"
In the Context Variable Considerations section of the Apex Developer Guide it is stated that for a before delete trigger:
Can update original object using an update DML operation: Allowed. The updates are saved before the object is deleted, so if the object is undeleted, the updates become visible.
Yet, the code below results in the exception System.FinalException: Record is read-only
Any ideas on how to accomplish this (Waypoint__c is a custom object from one of the trails modules. I added the text field field3__c to it)?
Can update original object using an update DML operation: Allowed. The updates are saved before the object is deleted, so if the object is undeleted, the updates become visible.
Yet, the code below results in the exception System.FinalException: Record is read-only
Any ideas on how to accomplish this (Waypoint__c is a custom object from one of the trails modules. I added the text field field3__c to it)?
if(Trigger.isDelete) { List<Waypoint__c> lw = new List<Waypoint__c> {}; for(Waypoint__c w : trigger.old) { lw.add(w); } for(Waypoint__c w : lw) { w.Field3__c = 'BeforeDel'; } update lw; }
All Answers
Try this:
Hope this helps!
Raj, I want to update the original object before it is deleted.
Could you please explain why your code worked and mine didn't? I had copied the records in the Trigger.old to a new list, isn't that the way it's supposed to be done?
Still don't understand why copying Trigger.old to a new list and updating this new list doesn't work.
To test this out, I wrote the code below And sure enought, the debug message is: 09:47:23:002 USER_DEBUG [12]|DEBUG|w.field1__c: Updated from lw2
The objects in the list above are pointing at the original w object and are not copies of w.