BeforeUpdate
Runs in before update, before the changes are saved, with the old and new values side by side.
Roles
- Populator: set, derive or clear fields, usually when another field changed.
- Validator: reject a change with an error message.
Register
Implement TriggerOrchestrator.BeforeUpdate on the orchestrator and return the handlers from beforeUpdateHandlers(). List order is run order. The trigger must list before update and call TriggerOrchestrator.run(new ContactTriggerOrchestrator()).
apex
public with sharing class ContactTriggerOrchestrator implements TriggerOrchestrator.BeforeUpdate {
public List<BeforeUpdate.Handler> beforeUpdateHandlers() {
return new List<BeforeUpdate.Handler>{ new ContactPopulator(), new ContactValidator() };
}
}Rules
- The old row is read-only. Writing to
getOldSObject()or callingaddErroron it throws aFinalExceptionthat nocatchstops. The update fails. - Updates re-enter. A later update of the same records in the transaction runs BeforeUpdate again. When a record comes back to a Populator after 3 passes, the run logs an error and the Populator keeps running. A Validator runs on every pass.
- Lookups hold only the Id.
((Contact) record.getNewSObject()).Accountis null. Declare a ParentQuery and readrecord.getNewParent(Contact.AccountId). - One role per class. A class that implements both roles runs only as a Populator. List Populators first, so Validators see the values they set.
WARNING
No DML. If a handler runs DML or publishes an event, the library throws. Set fields with record.put, and change other records from an AfterUpdate.Writer.
