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

# AfterDelete.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 ownUnitOfWorkOnAfterDelete();
}
```

* `ownUnitOfWorkOnAfterDelete()`: called once per chunk, when the handler list is built. Returns the unit this Writer registers into.

Only a [Writer](/after-delete/writer) uses it; a Dispatcher ignores it.

**Example**

::: code-group

```apex [Skeleton]
public with sharing class ContactWriter implements AfterDelete.Writer, AfterDelete.OwnUnitOfWork {
    public DML.Committable ownUnitOfWorkOnAfterDelete() {
        return new DML().userMode().identifier('ContactWriter');
    }

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

    public void writeOnAfterDelete(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 deleted row is still off-limits.** Updating it fails with `ENTITY_IS_DELETED` 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>')`.
:::
