---
url: https://trigger.beyondthecloud.dev/before-delete/add-ons/related-query.md
description: >-
  Query children, siblings or other records in a before delete Validator or
  Writer with one query per provider, while the rows are still linked.
---

# BeforeDelete.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, BeforeDelete.RecordsProvider> queryRelatedOnBeforeDelete();
}
```

* `queryRelatedOnBeforeDelete()`: 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 ContactValidator implements BeforeDelete.Validator, BeforeDelete.RelatedQuery {
    public Map<String, BeforeDelete.RecordsProvider> queryRelatedOnBeforeDelete() {
        return new Map<String, BeforeDelete.RecordsProvider>{ 'accountContacts' => new AccountContactsProvider() };
    }

    public Boolean addErrorOnBeforeDeleteWhen(TriggerTypes.DeleteRecord record) {
        Id accountId = ((Contact) record.getOldSObject()).AccountId;

        return accountId != null && record.getRelated('accountContacts').getAllWhereKeyEquals(accountId).size() == 1;
    }

    public void addErrorOnBeforeDelete(TriggerTypes.RejectableDeleteRecord record) {
        record.addError('The last contact of an account cannot be deleted.');
    }

    private with sharing class AccountContactsProvider implements BeforeDelete.RecordsProvider {
        public List<SObject> query(TriggerTypes.DeleteRecords 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.DeleteRecords 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}

* **An unknown name throws.** `getRelated` throws `TriggerTypes.TriggerLibException` when no provider has that name.
* **Counts include the records being deleted.** A query here still returns them. To count what will remain, add `Id NOT IN :records.getIds()`.
* **Last chance for cascade children.** Children removed by a cascade delete run no delete triggers of their own. Read them here.
* **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 nothing qualifies, so return an empty list from `query` when no record can qualify.

::: warning
Sharing can hide rows. A `with sharing` provider misses rows the user cannot see, so a guard can let a delete through. Use `without sharing` when it must see every row.
:::
