

It also writes a record away to a custom object called Audit_c for each Account deleted. In the example below we have a handler for the Account object that has some logic to make sure the Account isn’t referenced elsewhere before it can be deleted. We can now add a handler class that implements this interface. * method to accomplish any final operations such as creation or updates of other records. * This method is called once all records have been processed by the trigger. * This method is called iteratively for each record deleted during an AFTER Void afterUpdate(SObject oldSo, SObject so) * This method is called iteratively for each record updated during an AFTER Always put field validation in the 'After' methods in case another trigger * This method is called iteratively for each record inserted during an AFTER * This method is called iteratively for each record to be deleted during a BEFORE Void beforeUpdate(SObject oldSo, SObject so) * This method is called iteratively for each record to be updated during a BEFORE Never execute any SOQL/SOSL etc in this and other iterative methods. * This method is called iteratively for each record to be inserted during a BEFORE * This method is called prior to execution of an AFTER trigger. * any data required into maps prior execution of the trigger. * This method is called prior to execution of a BEFORE trigger. * Interface containing methods Trigger Handlers must implement to enforce best practice This interface contains the method signatures each handler must implement. We start by defining an Interface that provides the template for our trigger handlers. The trigger factory takes care of instantiating the handler and calling the handler methods in the right order.
For tidiness code#
Whenever we create a new trigger all we need to do is create a new handler class and add a line of code to the trigger itself to delegate the work to the trigger factory. We make use of a Factory class to instantiate the appropriate trigger handler. The trigger itself has almost no code in it. Each object will generally have one trigger handler and the trigger will specify the handler to use.

This pattern involves delegating work from the trigger to a structured Trigger Handler class.

While this goes against the grain of keeping the trigger code in one place you may have a need to deploy some temporary trigger code or you don’t want to refactor an existing handler.

Trigger Pattern for Tidy, Streamlined, Bulkified Triggers I thought I’d launch my blog by revisiting a post I wrote for the Cookbook.
