Skip to content

AfterUndelete.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 ownUnitOfWorkOnAfterUndelete();
}
  • ownUnitOfWorkOnAfterUndelete(): 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 AfterUndelete.Writer, AfterUndelete.OwnUnitOfWork {
    public DML.Committable ownUnitOfWorkOnAfterUndelete() {
        return new DML().userMode().identifier('ContactWriter');
    }

    public Boolean writeOnAfterUndeleteWhen(TriggerTypes.UndeleteRecord record) {
        return record.isNotNull(Contact.AccountId);
    }

    public void writeOnAfterUndelete(TriggerTypes.UndeleteRecord record, TriggerTypes.UnitOfWork unitOfWork) {
        unitOfWork.toInsert(new Task(WhatId = ((Contact) record.getNewSObject()).AccountId, Subject = 'Review contact'));
    }
}

Rules ​

  • 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. Add combineOnDuplicate() to merge a second toUpdate or toDelete of the same Id.
  • Each unit costs its own statements. It commits apart from the shared unit of work: one DML statement per operation and object type.

WARNING

Failed rows are not logged. With allowPartialSuccess(), a failing row does not throw. Read the results with commitHook or DML.retrieveResultFor('<identifier>').