---
url: https://trigger.beyondthecloud.dev/after-undelete/add-ons/related-query.md
description: >-
  Query children, siblings or configuration once per handler per run for
  restored records in an after undelete Writer or Dispatcher, and read them by
  key.
---

# AfterUndelete.RelatedQuery

Query children, siblings or other records once per handler per run, and read them per record with `record.getRelated(name)`.

**Signature**

```apex
public interface RelatedQuery {
    Map<String, AfterUndelete.RecordsProvider> queryRelatedOnAfterUndelete();
}
```

* `queryRelatedOnAfterUndelete()`: called at most once per handler per run, at this handler’s first `getRelated`. Returns provider name → `RecordsProvider`.

**Example**

::: code-group

```apex [Skeleton]
public with sharing class ContactWriter implements AfterUndelete.Writer, AfterUndelete.RelatedQuery {
    public Map<String, AfterUndelete.RecordsProvider> queryRelatedOnAfterUndelete() {
        return new Map<String, AfterUndelete.RecordsProvider>{ 'accountContacts' => new AccountContactsProvider() };
    }

    public Boolean writeOnAfterUndeleteWhen(TriggerTypes.UndeleteRecord record) {
        return record.isNotNull(Contact.AccountId);
    }

    public void writeOnAfterUndelete(TriggerTypes.UndeleteRecord record, TriggerTypes.UnitOfWork unitOfWork) {
        Id accountId = ((Contact) record.getNewSObject()).AccountId;
        Integer contactCount = record.getRelated('accountContacts').getAllWhereKeyEquals(accountId).size();

        unitOfWork.toInsert(new Task(WhatId = accountId, Subject = 'Contacts on the account: ' + contactCount));
    }

    private with sharing class AccountContactsProvider implements AfterUndelete.RecordsProvider {
        public List<SObject> query(TriggerTypes.UndeleteRecords records) {
            return [SELECT Id, AccountId FROM Contact WHERE AccountId IN :records.getIdsOf(Contact.AccountId)];
        }

        public String keyOf(SObject row) {
            return ((Contact) row).AccountId;
        }
    }
}
```

:::

## RecordsProvider {#records-provider}

**Signature**

```apex
public interface RecordsProvider {
    List<SObject> query(TriggerTypes.UndeleteRecords records);
    String keyOf(SObject row);
}
```

* `query(records)`: called once per provider, at the handler’s first `getRelated` with its name, with every record in the chunk. Returns the rows to index by key; `null` counts as no rows.
* `keyOf(row)`: called once per row that `query` returned. Returns the key that `getFirstWhereKeyEquals` and `getAllWhereKeyEquals` match; `null` leaves the row out of the index.

## Rules {#rules}

* **SOQL runs on the first read.** A provider queries when the handler first calls `getRelated` with its name. A read in a predicate queries even when no record qualifies, so return an empty list when no record can qualify.
* **SOQL sees the restored records.** Add `Id NOT IN :records.getIds()` to look only at other records.
* **Exact keys.** Keys are compared as text, case included. Normalize text keys the same way on both sides.
* **Unknown names throw.** `record.getRelated('<name>')` with a name the handler did not return throws `TriggerTypes.TriggerLibException`, even with ContinueOnError.

::: warning
Set sharing on the provider. Its SOQL runs under its own class's sharing keyword. An inner class does not take its outer class's keyword.
:::
