Versions Compared

Key

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

...

To use the framework, create an apex trigger as you would normally do. Kindly note, that the developer still needs to define the event that needs to be processed in the trigger itself. This includes the before and after events of specific trigger operation (insert, update, delete) that the programmer intend to catch as per business requirement.

  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
        }
    }
}

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];