---
url: https://trigger.beyondthecloud.dev/after-delete/writer.md
description: >-
  Create, update or delete other records after a delete - remove orphans, note
  the former parent, recompute a rollup - through a unit of work.
---

# AfterDelete.Writer

Change other records or publish platform events after the delete, through a unit of work.

**Signature**

```apex
public interface Writer extends AfterDelete.Handler {
    Boolean writeOnAfterDeleteWhen(TriggerTypes.DeleteRecord record);
    void writeOnAfterDelete(TriggerTypes.DeleteRecord record, TriggerTypes.UnitOfWork unitOfWork);
}
```

* `writeOnAfterDeleteWhen(record)`: called once per record in the chunk. Return `true` to write for the record.
* `writeOnAfterDelete(record, unitOfWork)`: called right after its predicate returns true, with this Writer’s unit of work.

**Example**

::: code-group

```apex [Skeleton]
public with sharing class ContactWriter implements AfterDelete.Writer {
    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'));
    }
}
```

```apex [Skip merge losers]
public with sharing class ContactRemovalTaskWriter implements AfterDelete.Writer, AfterDelete.PriorParentQuery {
    public Map<SObjectField, TriggerTypes.ParentFields> queryPriorParentsOnAfterDelete() {
        return new Map<SObjectField, TriggerTypes.ParentFields>{ Contact.AccountId => TriggerTypes.ParentFields.with(Account.OwnerId) };
    }

    public Boolean writeOnAfterDeleteWhen(TriggerTypes.DeleteRecord record) {
        return record.isNull(Contact.MasterRecordId) && record.getOldParent(Contact.AccountId) != null;
    }

    public void writeOnAfterDelete(TriggerTypes.DeleteRecord record, TriggerTypes.UnitOfWork unitOfWork) {
        Contact contactRecord = (Contact) record.getOldSObject();
        Account accountRecord = (Account) record.getOldParent(Contact.AccountId);

        unitOfWork.toInsert(new Task(WhatId = contactRecord.AccountId, OwnerId = accountRecord.OwnerId, Subject = 'Contact removed: ' + contactRecord.LastName));
    }
}
```

:::

## Rules {#rules}

* **Read the old row, not SOQL.** SOQL no longer finds the deleted row. Read its values from `getOldSObject()`, which is read-only.
* **Merge losers qualify.** Skip them as `[Skip merge losers]` does.
* **Register with `toInsert`, `toUpdate`, `toUpsert`, `toDelete` or `toPublish` (platform events).** They commit after the last handler, or right after this Writer with [OwnUnitOfWork](/after-delete/add-ons/own-unit-of-work) or [ContinueOnError](/after-delete/add-ons/continue-on-error). DML in the predicate or the action throws.
* **Writer wins.** A class that also implements `AfterDelete.Dispatcher` runs only as a Writer.

::: warning
Never register the deleted row. An update of it fails the commit with `ENTITY_IS_DELETED`, and a delete with `SELF_REFERENCE_FROM_TRIGGER`.
:::

## Test {#test}

```apex
@IsTest
static void writeOnAfterDeleteWithAccount() {
    // Setup
    Contact doe = new Contact(LastName = 'Doe', AccountId = new TriggerTypes.RandomIdGenerator().get(Account.SObjectType));

    DML.mock('triggerUow').allDmls();

    TriggerOrchestrator.mock().afterDeleteFor(ContactWriter.class).with(doe);

    // Test
    TriggerOrchestrator.runTestFor(new ContactWriter());

    // Verify
    Assert.areEqual(1, DML.retrieveResultFor('triggerUow').insertsOf(Task.SObjectType).records().size(), 'One task should be registered.');
}
```
