---
url: https://trigger.beyondthecloud.dev/before-delete/add-ons/own-unit-of-work.md
description: >-
  Give a before delete Writer its own DML Lib unit of work - user mode, sharing,
  partial success, statement order - committed right after that Writer.
---

# 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](/before-delete/writer) uses it; a Validator ignores it.

**Example**

::: code-group

```apex [Skeleton]
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 {#rules}

* **The shared unit.** Without this add-on or ContinueOnError, 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>')`.
:::
