Skip to content

AfterUpdate.PriorParentQuery ​

Read fields of the parent the old row pointed to, such as the previous owner, without SOQL in your handler.

Signature

apex
public interface PriorParentQuery {
    Map<SObjectField, TriggerTypes.ParentFields> queryPriorParentsOnAfterUpdate();
}
  • queryPriorParentsOnAfterUpdate(): 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.PriorParentQuery {
    public Map<SObjectField, TriggerTypes.ParentFields> queryPriorParentsOnAfterUpdate() {
        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.getOldParent(Contact.AccountId) != null;
    }

    public void writeOnAfterUpdate(TriggerTypes.UpdateRecord record, TriggerTypes.UnitOfWork unitOfWork) {
        Account previousAccount = (Account) record.getOldParent(Contact.AccountId);

        unitOfWork.toInsert(new Task(WhatId = previousAccount.Id, Subject = 'Contact left ' + previousAccount.Name));
    }
}
cls
public with sharing class AccountOwnerTransferWriter implements AfterUpdate.Writer, AfterUpdate.ParentQuery, AfterUpdate.PriorParentQuery, AfterUpdate.RelatedQuery, AfterUpdate.Bypassable {
    public static Boolean isBypassed = false;

    public Boolean bypassOnAfterUpdateWhen() {
        return AccountOwnerTransferWriter.isBypassed || System.isBatch();
    }

    public Map<SObjectField, TriggerTypes.ParentFields> queryParentsOnAfterUpdate() {
        return new Map<SObjectField, TriggerTypes.ParentFields>{ Account.OwnerId => TriggerTypes.ParentFields.with(User.Name) };
    }

    public Map<SObjectField, TriggerTypes.ParentFields> queryPriorParentsOnAfterUpdate() {
        return new Map<SObjectField, TriggerTypes.ParentFields>{ Account.OwnerId => TriggerTypes.ParentFields.with(User.Name) };
    }

    public Map<String, AfterUpdate.RecordsProvider> queryRelatedOnAfterUpdate() {
        return new Map<String, AfterUpdate.RecordsProvider>{ 'openOpportunities' => new AccountOpenOpportunitiesProvider() };
    }

    public Boolean writeOnAfterUpdateWhen(TriggerTypes.UpdateRecord record) {
        return record.isChanged(Account.OwnerId);
    }

    public void writeOnAfterUpdate(TriggerTypes.UpdateRecord record, TriggerTypes.UnitOfWork unitOfWork) {
        Account newAccount = (Account) record.getNewSObject();
        User oldOwner = (User) record.getOldParent(Account.OwnerId);
        User newOwner = (User) record.getNewParent(Account.OwnerId);
        String handoverNote = Date.today().format() + ' - account handed over from ' + oldOwner?.Name + ' to ' + newOwner?.Name;

        for (SObject row : record.getRelated('openOpportunities').getAllWhereKeyEquals(record.getId())) {
            Opportunity opportunity = (Opportunity) row;

            unitOfWork.toUpdate(new Opportunity(Id = opportunity.Id, OwnerId = newAccount.OwnerId, Description = this.notedDescription(handoverNote, opportunity.Description)));
        }
    }

    private String notedDescription(String handoverNote, String description) {
        return String.isBlank(description) ? handoverNote : handoverNote + '\n' + description;
    }

    private with sharing class AccountOpenOpportunitiesProvider implements AfterUpdate.RecordsProvider {
        public List<SObject> query(TriggerTypes.UpdateRecords records) {
            return [
                SELECT Id, AccountId, Description
                FROM Opportunity
                WHERE AccountId IN :records.getIds() AND IsClosed = FALSE
            ];
        }

        public String keyOf(SObject row) {
            return ((Opportunity) row).AccountId;
        }
    }
}

Rules ​

  • Read with getOldParent. Pass the lookup field you declared, such as getOldParent(Account.OwnerId). It is null when the old lookup was empty or the parent no longer exists.
  • Each side needs its own declaration. getOldParent needs PriorParentQuery and getNewParent needs ParentQuery, even when the lookup did not change. Implement both to compare the two parents.
  • Current values. The previous parent is queried now, so its fields show today's values.
  • At most one query per lookup per chunk. It runs when a handler first reads that lookup's parent. A lookup no handler reads costs nothing.

WARNING

Parents are read in system mode without sharing.