BeforeDelete.PriorParentQuery
Read fields of the parent the old row pointed to, such as a deleted contact's account, without SOQL in your handler.
Signature
apex
public interface PriorParentQuery {
Map<SObjectField, TriggerTypes.ParentFields> queryPriorParentsOnBeforeDelete();
}queryPriorParentsOnBeforeDelete(): called once per chunk, before the first handler runs. Returns lookup field → the parent fields to load.
Example
apex
public with sharing class ContactValidator implements BeforeDelete.Validator, BeforeDelete.PriorParentQuery {
public Map<SObjectField, TriggerTypes.ParentFields> queryPriorParentsOnBeforeDelete() {
return new Map<SObjectField, TriggerTypes.ParentFields>{ Contact.AccountId => TriggerTypes.ParentFields.with(Account.Name) };
}
public Boolean addErrorOnBeforeDeleteWhen(TriggerTypes.DeleteRecord record) {
return record.getOldParent(Contact.AccountId) != null;
}
public void addErrorOnBeforeDelete(TriggerTypes.RejectableDeleteRecord record) {
record.addError('Remove the contact from ' + ((Account) record.getOldParent(Contact.AccountId)).Name + ' first.');
}
}Rules
- Read by lookup field. Pass the lookup field you declared, such as
getOldParent(Contact.AccountId)orgetOldParent(Account.ParentId). - 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 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.
WARNING
Parents are read in system mode without sharing, so a handler can see records the user cannot.
