AfterDelete.OwnUnitOfWork
Give a Writer its own DML Lib unit of work, for user mode, sharing, partial success or your own statement order.
Signature
apex
public interface OwnUnitOfWork {
DML.Committable ownUnitOfWorkOnAfterDelete();
}ownUnitOfWorkOnAfterDelete(): called once per chunk, when the handler list is built. Returns the unit this Writer registers into.
Only a Writer uses it; a Dispatcher ignores it.
Example
apex
public with sharing class ContactWriter implements AfterDelete.Writer, AfterDelete.OwnUnitOfWork {
public DML.Committable ownUnitOfWorkOnAfterDelete() {
return new DML().userMode().identifier('ContactWriter');
}
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'));
}
}Rules
- The shared unit. Without this add-on or ContinueOnError, Writers share one unit in system mode, without sharing, all or none. It commits once, after the last handler.
- Commits at the Writer's turn. Your unit commits right after this Writer, only when at least one record qualified. Later handlers can query its rows.
- The deleted row is still off-limits. Updating it fails with
ENTITY_IS_DELETEDin any mode. - Wins over ContinueOnError. A Writer that implements both gets the unit it returns, not the automatic one.
WARNING
Partial success hides failures from the Logger. With allowPartialSuccess(), failed rows do not throw. Read them with DML.retrieveResultFor('<identifier>').
