---
url: https://trigger.beyondthecloud.dev/after-undelete.md
description: >-
  After undelete in Trigger Lib - react to records restored from the Recycle Bin
  with a Writer or a Dispatcher, and block a restore.
---

# AfterUndelete

Runs in **after undelete**, after records are restored from the Recycle Bin.

## Roles {#roles}

* [Writer](/after-undelete/writer): change other records or publish events through a unit of work.
* [Dispatcher](/after-undelete/dispatcher): make one bulk call per chunk, such as enqueueing a Queueable.

## Register {#register}

Implement `TriggerOrchestrator.AfterUndelete` on the orchestrator and return the handlers from `afterUndeleteHandlers()`. List order is run order. The trigger must list `after undelete` and call `TriggerOrchestrator.run(new ContactTriggerOrchestrator())`.

```apex
public with sharing class ContactTriggerOrchestrator implements TriggerOrchestrator.AfterUndelete {
    public List<AfterUndelete.Handler> afterUndeleteHandlers() {
        return new List<AfterUndelete.Handler>{ new ContactWriter(), new ContactDispatcher() };
    }
}
```

## Rules {#rules}

* **No Validator.** Salesforce has no before undelete event. To block a restore, call `record.getNewSObject().addError(…)` from a Writer or Dispatcher and list that handler first. The record stays in the Recycle Bin.
* **Lookups hold only the Id.** `((Contact) record.getNewSObject()).Account` is null. Declare a [ParentQuery](/after-undelete/add-ons/parent-query) and read `record.getNewParent(Contact.AccountId)`.
* **Only the top-level record's trigger runs.** Salesforce restores cascade-deleted children with their parent, but runs only the parent's after undelete trigger. Handle the children from the parent's handlers.
* **Called per trigger run.** `afterUndeleteHandlers()` runs once per trigger run. An Apex `undelete` of 1,000 records arrives in one run, not in chunks of 200.

::: warning
The restored row is read-only. Writing to `getNewSObject()` throws a `FinalException` that no `catch` stops. To change the record, register a `toUpdate` of its Id, which fires BeforeUpdate and AfterUpdate again.
:::
