AfterDelete.Writer
Change other records or publish platform events after the delete, through a unit of work.
Signature
apex
public interface Writer extends AfterDelete.Handler {
Boolean writeOnAfterDeleteWhen(TriggerTypes.DeleteRecord record);
void writeOnAfterDelete(TriggerTypes.DeleteRecord record, TriggerTypes.UnitOfWork unitOfWork);
}writeOnAfterDeleteWhen(record): called once per record in the chunk. Returntrueto write for the record.writeOnAfterDelete(record, unitOfWork): called right after its predicate returns true, with this Writer’s unit of work.
Example
apex
public with sharing class ContactWriter implements AfterDelete.Writer {
public Boolean writeOnAfterDeleteWhen(TriggerTypes.DeleteRecord record) {
return record.isNotNull(Contact.AccountId);
}
public void writeOnAfterDelete(TriggerTypes.DeleteRecord record, TriggerTypes.UnitOfWork unitOfWork) {
unitOfWork.toInsert(new Task(WhatId = ((Contact) record.getOldSObject()).AccountId, Subject = 'Review contact'));
}
}apex
public with sharing class ContactRemovalTaskWriter implements AfterDelete.Writer, AfterDelete.PriorParentQuery {
public Map<SObjectField, TriggerTypes.ParentFields> queryPriorParentsOnAfterDelete() {
return new Map<SObjectField, TriggerTypes.ParentFields>{ Contact.AccountId => TriggerTypes.ParentFields.with(Account.OwnerId) };
}
public Boolean writeOnAfterDeleteWhen(TriggerTypes.DeleteRecord record) {
return record.isNull(Contact.MasterRecordId) && record.getOldParent(Contact.AccountId) != null;
}
public void writeOnAfterDelete(TriggerTypes.DeleteRecord record, TriggerTypes.UnitOfWork unitOfWork) {
Contact contactRecord = (Contact) record.getOldSObject();
Account accountRecord = (Account) record.getOldParent(Contact.AccountId);
unitOfWork.toInsert(new Task(WhatId = contactRecord.AccountId, OwnerId = accountRecord.OwnerId, Subject = 'Contact removed: ' + contactRecord.LastName));
}
}Rules
- Read the old row, not SOQL. SOQL no longer finds the deleted row. Read its values from
getOldSObject(), which is read-only. - Merge losers qualify. Skip them as
[Skip merge losers]does. - Register with
toInsert,toUpdate,toUpsert,toDeleteortoPublish(platform events). They commit after the last handler, or right after this Writer with OwnUnitOfWork or ContinueOnError. DML in the predicate or the action throws. - Writer wins. A class that also implements
AfterDelete.Dispatcherruns only as a Writer.
WARNING
Never register the deleted row. An update of it fails the commit with ENTITY_IS_DELETED, and a delete with SELF_REFERENCE_FROM_TRIGGER.
Test
apex
@IsTest
static void writeOnAfterDeleteWithAccount() {
// Setup
Contact doe = new Contact(LastName = 'Doe', AccountId = new TriggerTypes.RandomIdGenerator().get(Account.SObjectType));
DML.mock('triggerUow').allDmls();
TriggerOrchestrator.mock().afterDeleteFor(ContactWriter.class).with(doe);
// Test
TriggerOrchestrator.runTestFor(new ContactWriter());
// Verify
Assert.areEqual(1, DML.retrieveResultFor('triggerUow').insertsOf(Task.SObjectType).records().size(), 'One task should be registered.');
}