BeforeUpdate.ParentQuery
Read fields of the record a lookup points to, such as a contact's current account, without SOQL in your handler.
Signature
apex
public interface ParentQuery {
Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnBeforeUpdate();
}queryParentsOnBeforeUpdate(): called once per chunk, before the first handler runs. Returns lookup field → the parent fields to load.
Example
apex
public with sharing class ContactPopulator implements BeforeUpdate.Populator, BeforeUpdate.ParentQuery {
public Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnBeforeUpdate() {
return new Map<SObjectField, TriggerTypes.ParentFields>{ Contact.AccountId => TriggerTypes.ParentFields.with(Account.Name) };
}
public Boolean populateOnBeforeUpdateWhen(TriggerTypes.UpdateRecord record) {
return record.isChanged(Contact.AccountId) && record.getNewParent(Contact.AccountId) != null;
}
public void populateOnBeforeUpdate(TriggerTypes.UpdateRecord record) {
record.put(Contact.Description, ((Account) record.getNewParent(Contact.AccountId)).Name);
}
}cls
public with sharing class OpportunityAccountMoveValidator implements BeforeUpdate.Validator, BeforeUpdate.ParentQuery, BeforeUpdate.PriorParentQuery {
public Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnBeforeUpdate() {
return new Map<SObjectField, TriggerTypes.ParentFields>{ Opportunity.AccountId => TriggerTypes.ParentFields.with(Account.Name) };
}
public Map<SObjectField, TriggerTypes.ParentFields> queryPriorParentsOnBeforeUpdate() {
return new Map<SObjectField, TriggerTypes.ParentFields>{ Opportunity.AccountId => TriggerTypes.ParentFields.with(Account.Name) };
}
public Boolean addErrorOnBeforeUpdateWhen(TriggerTypes.UpdateRecord record) {
return record.equals(Opportunity.StageName, 'Closed Won') && record.isChanged(Opportunity.AccountId);
}
public void addErrorOnBeforeUpdate(TriggerTypes.RejectableUpdateRecord record) {
Account oldAccount = (Account) record.getOldParent(Opportunity.AccountId);
Account newAccount = (Account) record.getNewParent(Opportunity.AccountId);
record.addError(
Opportunity.AccountId,
'This Opportunity is Closed Won and stays with ' +
(oldAccount?.Name ?? 'its original Account') +
'. It cannot be moved to ' +
(newAccount?.Name ?? 'another Account') +
'.'
);
}
}cls
public with sharing class ContactAccountTransferPopulator implements BeforeUpdate.Populator, BeforeUpdate.ParentQuery, BeforeUpdate.PriorParentQuery, BeforeUpdate.RecursionGuard {
public Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnBeforeUpdate() {
return new Map<SObjectField, TriggerTypes.ParentFields>{ Contact.AccountId => TriggerTypes.ParentFields.with(Account.Name) };
}
public Map<SObjectField, TriggerTypes.ParentFields> queryPriorParentsOnBeforeUpdate() {
return new Map<SObjectField, TriggerTypes.ParentFields>{ Contact.AccountId => TriggerTypes.ParentFields.with(Account.Name) };
}
public Integer maxRunsPerRecordOnBeforeUpdate() {
return 1;
}
public Boolean populateOnBeforeUpdateWhen(TriggerTypes.UpdateRecord record) {
return record.isChanged(Contact.AccountId) && !record.isChangedFrom(Contact.AccountId, null);
}
public void populateOnBeforeUpdate(TriggerTypes.UpdateRecord record) {
Contact newContact = (Contact) record.getNewSObject();
Account oldAccount = (Account) record.getOldParent(Contact.AccountId);
Account newAccount = (Account) record.getNewParent(Contact.AccountId);
String auditLine = Date.today().format() + ' - transferred from ' + (oldAccount?.Name ?? 'a deleted account') + ' to ' + (newAccount?.Name ?? 'no account') + '.';
record.put(Contact.Description, String.isBlank(newContact.Description) ? auditLine : newContact.Description + '\n' + auditLine);
}
}Rules
- Read by lookup field. Pass the lookup field you declared, such as
getNewParent(Contact.AccountId)orgetNewParent(Opportunity.AccountId). The parent is null when the lookup is empty or no record has that Id. - Later handlers see the new parent. When a Populator changes the lookup, handlers after it get the new parent. If the lookup was read before, a parent not loaded yet costs one more query.
- Only declared fields. The parent holds the declared fields and its
Id. Reading any other field throws anSObjectException. Add grandparent fields with.with('Owner', User.IsActive). - One query per lookup. Each declared lookup costs at most one SOQL query per chunk, when a handler first reads its parent. A lookup no handler reads costs nothing. A PriorParentQuery on the same lookup shares that query.
WARNING
Parents are read in system mode without sharing, so a handler can see records the user cannot.
