Skip to content

BeforeUpdate.Finalizer ​

Run code once per chunk with the records that qualified, such as a duplicate check across records.

Signature

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

Example

apex
public with sharing class ContactPopulator implements BeforeUpdate.Populator, BeforeUpdate.Finalizer {
    public Boolean populateOnBeforeUpdateWhen(TriggerTypes.UpdateRecord record) {
        return record.isChanged(Contact.Email) && record.isNotBlank(Contact.Email);
    }

    public void populateOnBeforeUpdate(TriggerTypes.UpdateRecord record) {
        record.put(Contact.Email, ((Contact) record.getNewSObject()).Email.trim().toLowerCase());
    }

    public void finalizeOnBeforeUpdate(TriggerTypes.UpdateRecords records) {
        Set<String> emails = new Set<String>();

        for (TriggerTypes.UpdateRecord record : records.getRecords()) {
            Contact contactRecord = (Contact) record.getNewSObject();

            if (!emails.add(contactRecord.Email)) {
                contactRecord.Email.addError('Another contact in this save has the same email.');
            }
        }
    }
}

Rules ​

  • Put it on a Populator. On a Validator, it receives only the records that Validator already rejected.
  • put works here. records.getRecords() returns UpdateRecords.
  • Reject with the field form. UpdateRecord has no addError, so call it on a field of getNewSObject(), as the Skeleton does.
  • Only this chunk. Other chunks of the same update are out of reach.

WARNING

No DML. If the Finalizer runs DML or publishes an event, the library throws, even with ContinueOnError.