function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bill5Bill5 

Trigger update object before delete

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables_considerations.htm


According to the above link a before delete trigger "can update an original object using an update DML operation nd the updates are saved before the object is deleted, so if the object is undeleted, the updates become visible". However, the contents of the trigger.old collection are read-only and trigger.new is only available in insert and update triggers. Therefore how can a objects value be modified as part of the trigger before delete?

 

I would like to be able to chage the value of one of the fields on an object when it is deleted.

 

 

Thanks

Bill

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Update by dml means that you would have to create a copy of the object from trigger.old, then execute the update call using that copy.

All Answers

bob_buzzardbob_buzzard

Update by dml means that you would have to create a copy of the object from trigger.old, then execute the update call using that copy.

This was selected as the best answer
jwhartfieldjwhartfield

But if you create a copy and then update it, won't you have one object in the recycle bin (the one that was passed into the trigger)  and a new one in the database?  It sounds like the objective is to change the value on a field as it is on it's way the recycle bin.

 

Unfortunately, once something is in the recycle bin, you cannot modify it.  For example, the following does not work, but I think it illustrates what you are trying to achieve:

 

trigger AcctDeleteTest on Account (after delete) {
	List<Account> accsForUpdate = [SELECT Name From Account WHERE id in :trigger.old AND IsDeleted = true ALL ROWS];
	
	for(account acc : accsForUpdate)
acc.Name = 'dead'; update accsForUpdate; // bombs here :( }

 

Maybe you can build some sort of future method that is called from the trigger to 'undelete' these records, update them, and then delete them again?

bob_buzzardbob_buzzard

I think the point is that until the trigger has completed the delete hasn't taken place.