What is a Trigger Handler?
A trigger handler is an object that processes the trigger logic.
Following the trigger best practices, we separate the logic from triggers into trigger handlers.
First, we delegate the logic from within triggers to trigger handlers.
trigger CaseTrigger on Case (
before insert,
before update,
before delete,
after insert,
after update,
after delete,
after undelete
) {
Trap.getInstance().start(); // Start the trap
}
Here we get the instance of Trap
, which is a trigger handler controller, and it will manage the lifecycle of trigger handlers and delegate the trigger execution context to the trigger handler.
Then we create a trigger handler like this:
public with sharing class CaseTrigger extends Trap.TriggerHandler {
public override void setUpBeforeInsert(Trap.BulkObject bulkObj) {
bulkObj.newStream
.filter(new CustomFilterFunc())
.subscribe(Trap.F.addError.apply('test error'));
}
}
For details on the streams and bulk objects here, please check out 'Trigger Execution' section.