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

# AfterInsert.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 ownUnitOfWorkOnAfterInsert();
}
```

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

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

**Example**

::: code-group

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

    public Boolean writeOnAfterInsertWhen(TriggerTypes.InsertRecord record) {
        return record.isNotNull(Contact.AccountId);
    }

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

:::

## Rules {#rules}

* **Commits at the Writer's turn.** The unit commits right after this Writer's Finalizer, when at least one record qualified. Later handlers can query its rows.
* **Costs its own statements.** It is never merged with the shared unit of work. Each commit costs at least one DML statement per operation and object type.
* **Duplicates throw.** Without `combineOnDuplicate()`, a second `toUpdate` or `toDelete` of the same Id throws at registration.
* **A failed commit is this Writer's error.** It is logged under the Writer's name and fails the insert, unless the Writer implements [ContinueOnError](/after-insert/add-ons/continue-on-error).

::: warning
With `allowPartialSuccess()`, failed rows do not throw and never reach `TriggerOrchestrator.Logger`. Read them with `DML.retrieveResultFor('<your identifier>')`.
:::
