AfterUpdate.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 ownUnitOfWorkOnAfterUpdate();
}ownUnitOfWorkOnAfterUpdate(): 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 AfterUpdate.Writer, AfterUpdate.OwnUnitOfWork {
public DML.Committable ownUnitOfWorkOnAfterUpdate() {
return new DML().userMode().identifier('ContactWriter');
}
public Boolean writeOnAfterUpdateWhen(TriggerTypes.UpdateRecord record) {
return record.isChanged(Contact.AccountId);
}
public void writeOnAfterUpdate(TriggerTypes.UpdateRecord record, TriggerTypes.UnitOfWork unitOfWork) {
unitOfWork.toInsert(new Task(WhatId = ((Contact) record.getNewSObject()).AccountId, Subject = 'Review contact'));
}
}apex
public with sharing class OpportunityLossReviewTaskWriter implements AfterUpdate.Writer, AfterUpdate.OwnUnitOfWork {
public DML.Committable ownUnitOfWorkOnAfterUpdate() {
return new DML().userMode().allowPartialSuccess().commitHook(new FailedTasks()).identifier('OpportunityLossReviewTaskWriter');
}
public Boolean writeOnAfterUpdateWhen(TriggerTypes.UpdateRecord record) {
return record.isChangedTo(Opportunity.StageName, 'Closed Lost');
}
public void writeOnAfterUpdate(TriggerTypes.UpdateRecord record, TriggerTypes.UnitOfWork unitOfWork) {
unitOfWork.toInsert(new Task(WhatId = record.getId(), Subject = 'Loss review'));
}
private class FailedTasks implements DML.Hook {
public void before() {
}
public void after(DML.Result result) {
for (DML.Error error : result.insertsOf(Task.SObjectType).errors()) {
System.debug(LoggingLevel.ERROR, error.message());
}
}
}
}Rules
- It commits right after this Writer, if a record qualified. Later handlers see its rows.
new DML()runs in user mode. The running user's object permissions, field-level security and sharing apply.- Failed rows are not logged. With
allowPartialSuccess(), read the failures in acommitHook, as the example does, or withDML.retrieveResultFor('<identifier>'). - Failed commits fail the update. The error is logged under the Writer's name. Add ContinueOnError to swallow it; the Writer still uses this unit.
WARNING
Without combineOnDuplicate(), a second toUpdate or toDelete of the same Id throws at registration.
