BeforeUpdate.PriorParentQuery
Read fields of the parent the old row pointed to, such as a contact's previous account, without SOQL in your handler.
Signature
apex
public interface PriorParentQuery {
Map<SObjectField, TriggerTypes.ParentFields> queryPriorParentsOnBeforeUpdate();
}queryPriorParentsOnBeforeUpdate(): 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.PriorParentQuery {
public Map<SObjectField, TriggerTypes.ParentFields> queryPriorParentsOnBeforeUpdate() {
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.getOldParent(Contact.AccountId) != null;
}
public void populateOnBeforeUpdate(TriggerTypes.UpdateRecord record) {
record.put(Contact.Description, 'Moved from ' + ((Account) record.getOldParent(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 with
getOldParent.record.getOldParent(Contact.AccountId)is null when the old lookup was empty or the parent has been deleted since. - Current field values. The previous parent is queried when the trigger runs, so its fields show their values now, not when the record pointed to it.
- One query for both sides. With a ParentQuery on the same lookup, both parents load in one query, and each gets every field either side declared.
TIP
Only need the old Id? Read getOldSObject(). No add-on is needed.
