Skip to content

AfterUpdate.Finalizer ​

Run code once per chunk with the records that qualified, such as one bulk write.

Signature

apex
public interface Finalizer {
    void finalizeOnAfterUpdate(TriggerTypes.UpdateRecords records);
}
  • finalizeOnAfterUpdate(records): called once per chunk, after this handler’s records, only if at least one record qualified; after the dispatch for a Dispatcher.

Example

apex
public with sharing class ContactWriter implements AfterUpdate.Writer, AfterUpdate.Finalizer {
    private TriggerTypes.UnitOfWork unitOfWork;

    public Boolean writeOnAfterUpdateWhen(TriggerTypes.UpdateRecord record) {
        return record.isChanged(Contact.AccountId);
    }

    public void writeOnAfterUpdate(TriggerTypes.UpdateRecord record, TriggerTypes.UnitOfWork unitOfWork) {
        this.unitOfWork = unitOfWork;
        unitOfWork.toInsert(new Task(WhatId = ((Contact) record.getNewSObject()).AccountId, Subject = 'Review contact'));
    }

    public void finalizeOnAfterUpdate(TriggerTypes.UpdateRecords records) {
        for (Id accountId : records.getIdsOf(Contact.AccountId)) {
            this.unitOfWork.toUpdate(new Account(Id = accountId, Description = 'Contacts changed'));
        }
    }
}

Rules ​

  • Queries see this update. A query here returns the trigger records with their saved new values.
  • Keep the unit of work in a field. The Finalizer gets no unit of work. Store the one from the action, as the Skeleton does. Those registrations commit with the rest.
  • Direct DML runs at once. DML here is not merged with the unit of work and costs its own statement.