---
url: https://trigger.beyondthecloud.dev/after-undelete/add-ons/parent-query.md
description: >-
  Read parent (lookup) fields of restored records in an after undelete Writer or
  Dispatcher without SOQL in the handler.
---

# AfterUndelete.ParentQuery

Read fields of the record a lookup points to, such as a restored contact's account, without SOQL in your handler.

**Signature**

```apex
public interface ParentQuery {
    Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnAfterUndelete();
}
```

* `queryParentsOnAfterUndelete()`: called once per chunk, before the first handler runs. Returns lookup field → the parent fields to load.

**Example**

::: code-group

```apex [Skeleton]
public with sharing class ContactWriter implements AfterUndelete.Writer, AfterUndelete.ParentQuery {
    public Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnAfterUndelete() {
        return new Map<SObjectField, TriggerTypes.ParentFields>{ Contact.AccountId => TriggerTypes.ParentFields.with(Account.Name) };
    }

    public Boolean writeOnAfterUndeleteWhen(TriggerTypes.UndeleteRecord record) {
        return record.getNewParent(Contact.AccountId) != null;
    }

    public void writeOnAfterUndelete(TriggerTypes.UndeleteRecord record, TriggerTypes.UnitOfWork unitOfWork) {
        Account accountRecord = (Account) record.getNewParent(Contact.AccountId);

        unitOfWork.toInsert(new Task(WhatId = accountRecord.Id, Subject = 'Review contact of ' + accountRecord.Name));
    }
}
```

:::

## Rules {#rules}

* **Read by lookup field.** Pass the lookup field you declared, such as `getNewParent(Contact.AccountId)` or `getNewParent(Contact.OwnerId)`.
* **Check for null.** The parent is null when the lookup is empty or no record has that Id.
* **Declare every field you read.** Reading a field that no handler declared throws an `SObjectException`. Add grandparent fields with `.with('Owner', User.IsActive)`.
* **One query per chunk.** The first parent a handler reads runs one SOQL query on the restored records, which loads every declared parent. A chunk that reads no parent costs nothing.

::: warning
Parents are read in system mode without sharing, so a handler can see records the user cannot.
:::
