---
url: https://trigger.beyondthecloud.dev/after-insert/add-ons/parent-query.md
description: >-
  Read parent (lookup) fields in an after insert Writer or Dispatcher without
  writing SOQL.
---

# AfterInsert.ParentQuery

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

**Signature**

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

* `queryParentsOnAfterInsert()`: 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 AfterInsert.Writer, AfterInsert.ParentQuery {
    public Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnAfterInsert() {
        return new Map<SObjectField, TriggerTypes.ParentFields>{ Contact.AccountId => TriggerTypes.ParentFields.with(Account.Name) };
    }

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

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

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

```cls \[In the action]
public with sharing class AccountWelcomeTaskWriter implements AfterInsert.Writer, AfterInsert.ParentQuery, AfterInsert.ContinueOnError {
    public Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnAfterInsert() {
        return new Map<SObjectField, TriggerTypes.ParentFields>{ Account.OwnerId => TriggerTypes.ParentFields.with(User.Name, User.Email) };
    }

    public Boolean writeOnAfterInsertWhen(TriggerTypes.InsertRecord record) {
        return record.startsWith(Account.Type, 'Customer');
    }

    public void writeOnAfterInsert(TriggerTypes.InsertRecord record, TriggerTypes.UnitOfWork unitOfWork) {
        Account newAccount = (Account) record.getNewSObject();
        User owner = (User) record.getNewParent(Account.OwnerId);

        unitOfWork.toInsert(
            new Task(
                WhatId = record.getId(),
                OwnerId = newAccount.OwnerId,
                Subject = 'Onboarding call - ' + newAccount.Name,
                Description = 'New customer assigned to ' + owner?.Name + ' (' + owner?.Email + '). Confirm the billing address and the primary contact.',
                ActivityDate = Date.today().addDays(3),
                Priority = 'High',
                Status = 'Not Started'
            )
        );
    }
}

```

```cls \[Grandparent in the predicate]
public with sharing class ContactOwnerAlignmentWriter implements AfterInsert.Writer, AfterInsert.ParentQuery, AfterInsert.Bypassable {
    public static Boolean isBypassed = false;

    public Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnAfterInsert() {
        return new Map<SObjectField, TriggerTypes.ParentFields>{ Contact.AccountId => TriggerTypes.ParentFields.with(Account.OwnerId).with('Owner', User.IsActive) };
    }

    public Boolean bypassOnAfterInsertWhen() {
        return ContactOwnerAlignmentWriter.isBypassed;
    }

    public Boolean writeOnAfterInsertWhen(TriggerTypes.InsertRecord record) {
        Contact newContact = (Contact) record.getNewSObject();
        Account account = (Account) record.getNewParent(Contact.AccountId);

        return account?.Owner?.IsActive == true && account.OwnerId != newContact.OwnerId;
    }

    public void writeOnAfterInsert(TriggerTypes.InsertRecord record, TriggerTypes.UnitOfWork unitOfWork) {
        Account account = (Account) record.getNewParent(Contact.AccountId);

        unitOfWork.toUpdate(new Contact(Id = record.getId(), OwnerId = account.OwnerId));
    }
}

```

:::

## Rules {#rules}

* **Read by lookup field.** Pass the lookup field you declared, such as `getNewParent(Contact.AccountId)` or `getNewParent(Account.OwnerId)`.
* **Check for null.** The parent is null when the lookup is empty or no record has that Id.
* **Only declared fields.** The parent holds the declared fields and its `Id`. Reading any other field 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 saved 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.
:::
