Skip to content

AfterDelete.OwnUnitOfWork

Gives a Writer its own DML Lib unit of work, committed right after that Writer. Use it for user mode, sharing, partial success or your own statement order.

Interface

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

    public Boolean writeOnAfterDeleteWhen(TriggerHandler.DeleteRecord record) {
        return record.isNotNull(Contact.AccountId);
    }

    public void writeOnAfterDelete(TriggerHandler.DeleteRecord record, TriggerHandler.UnitOfWork unitOfWork) {
        unitOfWork.toInsert(new Task(WhatId = ((Contact) record.getOldSObject()).AccountId, Subject = 'Review contact'));
    }
}

Good to Know

  • The default unit. Without this add-on, 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_DELETED in any mode.
  • Partial success hides failures from the Logger. With allowPartialSuccess(), failed rows do not throw. Read them with DML.retrieveResultFor('<identifier>').
  • Wins over ContinueOnError. A Writer that implements both gets the unit it returns, not a private one.