Skip to content

BeforeDelete.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 ownUnitOfWorkOnBeforeDelete();
}
  • ownUnitOfWorkOnBeforeDelete(): called once per chunk, when the handler list is built. Returns the unit this Writer registers into.

Only a Writer uses it; a Validator ignores it.

Example

apex
public with sharing class ContactWriter implements BeforeDelete.Writer, BeforeDelete.OwnUnitOfWork {
    public DML.Committable ownUnitOfWorkOnBeforeDelete() {
        return new DML().userMode().identifier('ContactWriter');
    }

    public Boolean writeOnBeforeDeleteWhen(TriggerTypes.DeleteRecord record) {
        return record.isNotNull(Contact.AccountId);
    }

    public void writeOnBeforeDelete(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, 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 records being deleted are still off-limits. A toDelete of one fails with SELF_REFERENCE_FROM_TRIGGER in 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>').