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
apex
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
- Read by lookup field. Pass the lookup field you declared, such as
getNewParent(Contact.AccountId)orgetNewParent(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.
