AfterDelete
Runs in after delete, after records are deleted and before the transaction commits.
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.AfterDelete on the orchestrator and return the handlers from afterDeleteHandlers(). List order is run order. The trigger must list after delete and call TriggerOrchestrator.run(new ContactTriggerOrchestrator()).
apex
public with sharing class ContactTriggerOrchestrator implements TriggerOrchestrator.AfterDelete {
public List<AfterDelete.Handler> afterDeleteHandlers() {
return new List<AfterDelete.Handler>{ new ContactWriter(), new ContactDispatcher() };
}
}Rules
- The rows are gone.
getId()returns the deleted Id, but SOQL no longer finds the row. Key queries by the old lookups:records.getIdsOf(Contact.AccountId). - Merge losers arrive here. The records that lose a merge fire the delete triggers with
MasterRecordIdset. Skip them withrecord.isNull(Contact.MasterRecordId). - Stop deletes in BeforeDelete.
getOldSObject().addError(…)here rolls the delete back, but only after earlier handlers did their work. Use a BeforeDelete.Validator. - One role per class. A class that implements both roles runs only as a Writer.
WARNING
Cascade deletes never arrive. Records that the platform deletes with their parent, such as master-detail children, fire no delete triggers. Put that logic on the parent's delete.
