I’ve been writing Apex triggers for a while, and recently I ran across two ways to architect triggers that are different from how I used to do things. Both methods have one main benefit–they push all your logic to your classes, and allow the trigger to do just one job–invoke code on data change.

Here’s a trigger architected the first way:

trigger MyTrigger on MyObject__c (before insert, before update, after insert, after update) {

    if(trigger.isBefore){
        if(trigger.isInsert){
            MyObjectInsertBefore myInsertBefore = new MyObjectInsertBefore(Trigger.new);
        }
        if(trigger.isUpdate){
            MyObjectUpdateBefore myUpdateBefore = new MyObjectUpdateBefore(Trigger.old,Trigger.new);
        }
    }
    
    if(trigger.isAfter){
        if(trigger.isInsert){
            MyObjectInsertAfter myInsertAfter = new MyObjectInsertAfter(Trigger.new);
        }
        if(Trigger.isUpdate){
        	MyObjectUpdateAfter myUpdateAfter = new MyObjectUpdateAfter(Trigger.old,Trigger.new);
        }
    }
}

As you can see from the trigger, all that we’re doing is invoking the correct class based on the starting criteria–are we an insert or an update, and are we before or after? We’ve got a separate class for each of the four cases.

Here’s what two of those classes might look like:

public class MyObjectInsertBefore {
	
	//create your variables and data structures

    //constructor
    public MyObjectInsertBefore(){
    	
    }
    
    //constructor accepting a list of myObjects
    public MyObjectInsertBefore(MyObject__c[] myObjects){
        //call whatever methods you need to get the job done
        someMethod(myObjects);
        .
        .
        .
    }
}
public class MyObjectUpdateBefore {
	
	//create your variables and data structures

    //constructor
    public MyObjectUpdateBefore(){
    	
    }
    
    //constructor accepting a list of myObjects
    public MyObjectUpdateBefore(MyObject__c[] myOldObjects,MyObject__c[] myNewObjects){
        //call whatever methods you need to get the job done
        someMethod(myOldObjects,myNewObjects);
        .
        .
        .
    }
}

Key to making this efficient is to have shared methods in a MyObjectUtil class, so that similar operations can be reused across your classes.

Here’s a trigger invoked the second way:

trigger MyTrigger on MyObject__c (before insert, before update, after insert, after update) {

	public enum triggerAction {beforeInsert, beforeUpdate, afterInsert, afterUpdate}
  
    if(Trigger.isInsert && Trigger.isBefore){
        MyObjectClass process = new MyObjectClass(Trigger.new, Trigger.old, triggerAction.beforeInsert);
    }
    if( Trigger.isAfter && Trigger.isInsert ){
        MyObjectClass process = new MyObjectClass(Trigger.new, Trigger.old, triggerAction.afterInsert);
    }
    if(Trigger.isUpdate && Trigger.isBefore){
        MyObjectClass process = new MyObjectClass(Trigger.new, Trigger.old, triggerAction.beforeUpdate);
    }
    if( Trigger.isAfter && Trigger.isUpdate ){
        MyObjectClass process = new MyObjectClass(Trigger.new, Trigger.old, triggerAction.afterUpdate);
    }     
}

This trigger sends the old and new sets over to a class and passes the way that this was invoked via an enum value (look up enum in the Apex docs if you haven’t used them–I had to). There is only one class, and then the class sorts out what should happen based on the data:

public class MyObjectClass {

	public MyObjectClass()
	{
	}
	
	public enum triggeredAction {beforeInsert, beforeUpdate, afterInsert, afterUpdate}

	public MyObjectClass(MyObject__c[] myObjects, MyObject__c[] myOldObjects, triggeredAction ta) 
	{	

		// BEFORE INSERT
		if (ta==triggeredAction.beforeInsert) 
		{
			someBeforeInsertMethod()
		}
		
		// BEFORE UPDATE
		if (ta==triggeredAction.beforeUpdate) 
		{
			someBeforeUpdateMethod();
		}
		
		// AFTER INSERT
		if (ta==triggeredAction.afterInsert) 
		{
			someAfterInsertMethod()
		}
		
		// AFTER UPDATE
		if (ta==triggeredAction.afterUpdate) 
		{
			someAfterUpdateMethod();
		}

	}
}

You can see that you’re basically accomplishing the same thing–getting the class to invoke the right methods based on the invocation, but that we’re doing it in two different ways. In the first example, we’ve got a proliferation of classes, and each class is very simple. In the second example, we’ve got one class that is more complex as it has to handle many types of invocation.

I ran across the second example in the spring and really liked it. Then I ran across the first example last month and now it’s my favorite. I’m going to try to write new triggers that way and see how they go.

Thanks to Matt and Mike for architecting your code in these really interesting ways! I hope you, like me, love seeing how other people do things–it can really improve the way you code so very quickly.