AfterUpdate.ParentQuery
Read fields of the record a lookup points to, such as an opportunity's account, without SOQL in your handler.
Signature
apex
public interface ParentQuery {
Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnAfterUpdate();
}queryParentsOnAfterUpdate(): 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 AfterUpdate.Writer, AfterUpdate.ParentQuery {
public Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnAfterUpdate() {
return new Map<SObjectField, TriggerTypes.ParentFields>{ Contact.AccountId => TriggerTypes.ParentFields.with(Account.Name) };
}
public Boolean writeOnAfterUpdateWhen(TriggerTypes.UpdateRecord record) {
return record.isChanged(Contact.AccountId) && record.getNewParent(Contact.AccountId) != null;
}
public void writeOnAfterUpdate(TriggerTypes.UpdateRecord 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
public with sharing class OpportunityWinTaskWriter implements AfterUpdate.Writer, AfterUpdate.ParentQuery, AfterUpdate.ContinueOnError {
public Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnAfterUpdate() {
return new Map<SObjectField, TriggerTypes.ParentFields>{ Opportunity.AccountId => TriggerTypes.ParentFields.with(Account.Name) };
}
public Boolean writeOnAfterUpdateWhen(TriggerTypes.UpdateRecord record) {
return record.isChangedTo(Opportunity.StageName, 'Closed Won');
}
public void writeOnAfterUpdate(TriggerTypes.UpdateRecord record, TriggerTypes.UnitOfWork unitOfWork) {
Opportunity newOpportunity = (Opportunity) record.getNewSObject();
Account account = (Account) record.getNewParent(Opportunity.AccountId);
unitOfWork.toInsert(
new Task(
WhatId = record.getId(),
OwnerId = newOpportunity.OwnerId,
Subject = 'Onboarding kick-off: ' + (account?.Name ?? newOpportunity.Name),
Description = 'Won on ' + Date.today().format() + '. Agree the implementation plan with the customer.',
ActivityDate = Date.today().addDays(7),
Priority = 'High',
Status = 'Not Started'
)
);
}
}cls
public with sharing class OpportunityAccountTypeWriter implements AfterUpdate.Writer, AfterUpdate.ParentQuery, AfterUpdate.RecursionGuard {
public Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnAfterUpdate() {
return new Map<SObjectField, TriggerTypes.ParentFields>{ Opportunity.AccountId => TriggerTypes.ParentFields.with(Account.Type) };
}
public Integer maxRunsPerRecordOnAfterUpdate() {
return 1;
}
public Boolean writeOnAfterUpdateWhen(TriggerTypes.UpdateRecord record) {
if (!record.isChangedTo(Opportunity.StageName, 'Closed Won')) {
return false;
}
Account account = (Account) record.getNewParent(Opportunity.AccountId);
return account != null && account.Type != 'Customer - Direct';
}
public void writeOnAfterUpdate(TriggerTypes.UpdateRecord record, TriggerTypes.UnitOfWork unitOfWork) {
Opportunity newOpportunity = (Opportunity) record.getNewSObject();
unitOfWork.toUpdate(new Account(Id = newOpportunity.AccountId, Type = 'Customer - Direct'));
}
}Rules
- Read by lookup field. Pass the lookup field you declared, such as
getNewParent(Opportunity.AccountId)orgetNewParent(Opportunity.OwnerId). - Check for null. The parent is null when the lookup is empty or no record has that Id.
- Declare every field you read. The parent holds only the fields the active handlers declared. 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 query on the trigger 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.
