BeforeInsert
Runs in before insert, before new records are saved.
Roles
- Populator: set, default or normalize fields on the new record.
- Validator: reject a record with an error message.
Register
Implement TriggerOrchestrator.BeforeInsert on the orchestrator and return the handlers from beforeInsertHandlers(). List order is run order. The trigger must list before insert and call TriggerOrchestrator.run(new ContactTriggerOrchestrator()).
apex
public with sharing class ContactTriggerOrchestrator implements TriggerOrchestrator.BeforeInsert {
public List<BeforeInsert.Handler> beforeInsertHandlers() {
return new List<BeforeInsert.Handler>{ new ContactPopulator(), new ContactValidator() };
}
}Rules
- No Id yet.
record.getId()is null andrecords.getIds()is empty. Never key a map by the record Id here. - 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.
- Called per chunk.
beforeInsertHandlers()runs for every chunk of up to 200 records. Handlers created there start with empty instance fields each time; static fields last the whole transaction.
WARNING
No DML. If a handler runs DML or publishes an event, the library throws. Change other records from an AfterInsert.Writer.
