AfterUndelete
Runs in after undelete, after records are restored from the Recycle Bin.
Roles
- Writer: change other records or publish events through a unit of work.
- Dispatcher: make one bulk call per chunk, such as enqueueing a Queueable.
Register
Implement TriggerOrchestrator.AfterUndelete on the orchestrator and return the handlers from afterUndeleteHandlers(). List order is run order. The trigger must list after undelete and call TriggerOrchestrator.run(new ContactTriggerOrchestrator()).
apex
public with sharing class ContactTriggerOrchestrator implements TriggerOrchestrator.AfterUndelete {
public List<AfterUndelete.Handler> afterUndeleteHandlers() {
return new List<AfterUndelete.Handler>{ new ContactWriter(), new ContactDispatcher() };
}
}Rules
- No Validator. Salesforce has no before undelete event. To block a restore, call
record.getNewSObject().addError(…)from a Writer or Dispatcher and list that handler first. The record stays in the Recycle Bin. - Lookups hold only the Id.
((Contact) record.getNewSObject()).Accountis null. Declare a ParentQuery and readrecord.getNewParent(Contact.AccountId). - Only the top-level record's trigger runs. Salesforce restores cascade-deleted children with their parent, but runs only the parent's after undelete trigger. Handle the children from the parent's handlers.
- Called per trigger run.
afterUndeleteHandlers()runs once per trigger run. An Apexundeleteof 1,000 records arrives in one run, not in chunks of 200.
WARNING
The restored row is read-only. Writing to getNewSObject() throws a FinalException that no catch stops. To change the record, register a toUpdate of its Id, which fires BeforeUpdate and AfterUpdate again.
