AfterUndelete.OwnUnitOfWork
Gives a Writer its own DML Lib unit of work instead of the shared one. Use it for user mode, partial success or a custom statement order.
Interface
apex
public class AfterUndelete {
public interface OwnUnitOfWork {
DML.Committable ownUnitOfWorkOnAfterUndelete();
}
}ownUnitOfWorkOnAfterUndelete(): called once per chunk, when the handler list is built, even for a handler that is then bypassed. Returns the unit this Writer registers into.
Only a Writer uses it; a Dispatcher ignores it.
Example
apex
public with sharing class ContactWriter implements AfterUndelete.Writer, AfterUndelete.OwnUnitOfWork {
public DML.Committable ownUnitOfWorkOnAfterUndelete() {
return new DML().userMode().identifier('ContactWriter');
}
public Boolean writeOnAfterUndeleteWhen(TriggerHandler.UndeleteRecord record) {
return record.isNotNull(Contact.AccountId);
}
public void writeOnAfterUndelete(TriggerHandler.UndeleteRecord record, TriggerHandler.UnitOfWork unitOfWork) {
unitOfWork.toInsert(new Task(WhatId = ((Contact) record.getNewSObject()).AccountId, Subject = 'Review contact'));
}
}Good to Know
- Commits at the Writer's turn. The unit commits right after this Writer and its Finalizer, only when a record qualified. Later handlers can query what it wrote.
- A plain
new DML()runs in user mode. It also inherits sharing and throws on a duplicate registration. AddcombineOnDuplicate()to merge a secondtoUpdateortoDeleteof the same Id. - Failed rows are not logged. With
allowPartialSuccess(), a failing row does not throw. Read the results withcommitHookorDML.retrieveResultFor('<identifier>'). - Each unit costs its own statements. It commits apart from the shared unit: one DML statement per operation and object type.
- Getter errors fail the restore. An exception in
ownUnitOfWorkOnAfterUndelete()is not logged.
