BeforeInsert.ParentQuery
Read fields of the record a lookup points to, such as a contact's account, without SOQL in your handler.
Signature
apex
public interface ParentQuery {
Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnBeforeInsert();
}queryParentsOnBeforeInsert(): 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 BeforeInsert.Populator, BeforeInsert.ParentQuery {
public Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnBeforeInsert() {
return new Map<SObjectField, TriggerTypes.ParentFields>{ Contact.AccountId => TriggerTypes.ParentFields.with(Account.Name) };
}
public Boolean populateOnBeforeInsertWhen(TriggerTypes.InsertRecord record) {
return record.getNewParent(Contact.AccountId) != null;
}
public void populateOnBeforeInsert(TriggerTypes.InsertRecord record) {
record.put(Contact.Description, ((Account) record.getNewParent(Contact.AccountId)).Name);
}
}cls
public with sharing class AccountParentDefaultsPopulator implements BeforeInsert.Populator, BeforeInsert.ParentQuery {
public Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnBeforeInsert() {
return new Map<SObjectField, TriggerTypes.ParentFields>{ Account.ParentId => TriggerTypes.ParentFields.with(Account.Industry, Account.AccountSource) };
}
public Boolean populateOnBeforeInsertWhen(TriggerTypes.InsertRecord record) {
return record.isNotNull(Account.ParentId) && (record.isBlank(Account.Industry) || record.isBlank(Account.AccountSource));
}
public void populateOnBeforeInsert(TriggerTypes.InsertRecord record) {
Account parent = (Account) record.getNewParent(Account.ParentId);
if (parent == null) {
return;
}
if (record.isBlank(Account.Industry)) {
record.put(Account.Industry, parent.Industry);
}
if (record.isBlank(Account.AccountSource)) {
record.put(Account.AccountSource, parent.AccountSource);
}
}
}cls
public with sharing class AccountParentTypeValidator implements BeforeInsert.Validator, BeforeInsert.ParentQuery {
public Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnBeforeInsert() {
return new Map<SObjectField, TriggerTypes.ParentFields>{ Account.ParentId => TriggerTypes.ParentFields.with(Account.Name, Account.Type) };
}
public Boolean addErrorOnBeforeInsertWhen(TriggerTypes.InsertRecord record) {
Account parent = (Account) record.getNewParent(Account.ParentId);
return record.isNotNull(Account.ParentId) && parent?.Type == 'Prospect';
}
public void addErrorOnBeforeInsert(TriggerTypes.RejectableInsertRecord record) {
Account parent = (Account) record.getNewParent(Account.ParentId);
record.addError(Account.ParentId, 'Parent account "' + parent.Name + '" is still a Prospect and cannot head a hierarchy. Qualify it first.');
}
}Rules
- Read by lookup field. Pass the lookup field you declared, such as
getNewParent(Contact.AccountId)orgetNewParent(Account.ParentId). 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. A Populator that changes it after that read can add one.
WARNING
Parents are read in system mode without sharing, so a handler can see records the user cannot.
