Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Create the handler class by extending the USYD_TriggerHandlerBase class.

  2. Create include a constructor method which carries a String parameter to pass/contain the trigger name (which can be automatically determined from the trigger body).

  3. Override the applicable events (methods) that need to be implemented/executed (i.e. beforeInsert, beforeUpdate, etc.).

  4. Write the business logic. It is recommended to have the business logic written in a separate method and invoke it from the overridden event/method, rather than in the event itself. This promotes separation of event invocation from business logic. The developer may also opt to have business logic written in a separate class and just invoke it from the applicable events.
    Sample Trigger Handler Class:

    Code Block
    languagejava
    //AccountTriggerHandler.cls
    public class AccountTriggerHandler extends USYD_TriggerHandlerBase{
        //constructor as explained in step #2
        public TriggerTestTriggerHandler(String triggerName) {
            super(triggerName);
        }
        
        //overridden events as described in step #3
    	public override void beforeInsert(List<sObject> newRecords){
            for (Account record : (Account[])newRecords) {
                //invoke before insert business logic as described in step #4
            }
        }
        
        //overridden events as described in step #3
    	public override void beforeUpdate(List<sObject> oldRecords, List<sObject> newRecords, Map<ID, sObject> oldRecordMap, Map<ID, sObject> newRecordMap){
            for (Account record : (Account[])newRecords) {
                //invoke before update business logic as described in step #4
            }
        }
    }
  5. Create the apex trigger.

  6. In the trigger code, determine the name of the current trigger by having the this code just before invoking the trigger handler class:
    String triggerName = String.valueOf(this).split(':')[0];

  7. Below the line that determines the trigger name, define and instantiate an instance of the trigger that you have written.

  8. Execute the logic by invoking the trigger handler instance execute() method.
    Sample Trigger:

    Code Block
    languagejava
    //AccountTrigger.apxt
    trigger AccountTrigger on Account(before insert, before update) {
        String triggerName = String.valueOf(this).split(':')[0];
    	TriggerTestTriggerHandler handler = new TriggerTestTriggerHandler(triggerName);
        handler.execute();
    }