Skip to content

Instantly share code, notes, and snippets.

@abhinavguptas
Created January 2, 2012 09:40
Show Gist options
  • Save abhinavguptas/1550055 to your computer and use it in GitHub Desktop.
Save abhinavguptas/1550055 to your computer and use it in GitHub Desktop.
Yet Another Apex Trigger Template
/**
After insert handler on task Sobject
*/
public class TaskAfterInsertHandler implements Triggers.Handler {
public void handle() {
System.debug(LoggingLevel.INFO, 'Post insert handling ' + Trigger.new );
}
}
/**
After update handler on Task Sobject
*/
public class TaskAfterUpdateHandler implements Triggers.Handler {
// Prevent trigger from running twice on updates
// Ref : http://raydehler.com/cloud/clod/prevent-an-apex-trigger-from-executing-twice.html
static boolean firstRunForUpdate = true;
public void handle() {
if (firstRunForUpdate) {
System.debug(LoggingLevel.INFO, 'Post Update handling, NewItems: ' + Trigger.newMap + ', OldItems: ' + Trigger.oldMap );
}
}
}
/**
Before insert handler on task Sobject
*/
public class TaskBeforeInsertHandler implements Triggers.Handler {
public void handle() {
System.debug(LoggingLevel.INFO, 'Pre insert handling ' + Trigger.new );
}
}
/**
Before insert handler on task Sobject. This is SECOND before insert handler on the same Task Sobject
*/
public class TaskBeforeInsertHandler2 implements Triggers.Handler {
public void handle() {
System.debug(LoggingLevel.INFO, '2. Pre insert handling ' + Trigger.new );
}
}
/**
Trigger on Task Sobject
*/
trigger TaskTrigger on Task (after delete, after insert, after undelete,
after update, before delete,
before insert, before update) {
new Triggers()
.bind(Triggers.Evt.afterinsert, new TaskAfterInsertHandler())
// bind first before insert handler
.bind(Triggers.Evt.beforeinsert, new TaskBeforeInsertHandler())
// bind another before insert handler, that will execute after the first one.
// All you need to do is add handlers in the correct order here
.bind(Triggers.Evt.beforeinsert, new TaskBeforeInsertHandler2())
.bind(Triggers.Evt.afterupdate, new TaskAfterUpdateHandler())
// Done with adding all handlers, lets call the manage now for once only
.manage();
}
public class Triggers {
/**
Enum representing each of before/after CRUD events on Sobjects
*/
public enum Evt {
afterdelete, afterinsert, afterundelete,
afterupdate, beforedelete, beforeinsert, beforeupdate
}
/**
Simplistic handler to implement on any of the event. It doesn't requires or enforces any patter except the
method name to be "handle()", a developer is free to use any Trigger context variable or reuse any other
apex class here.
*/
public interface Handler {
void handle();
}
// Internal mapping of handlers
Map<String, List<Handler>> eventHandlerMapping = new Map<String, List<Handler>>();
/**
Core API to bind handlers with events
*/
public Triggers bind(Evt event, Handler eh) {
List<Handler> handlers = eventHandlerMapping.get(event.name());
if (handlers == null) {
handlers = new List<Handler>();
eventHandlerMapping.put(event.name(), handlers);
}
handlers.add(eh);
return this;
}
/**
Invokes correct handlers as per the context of trigger and available registered handlers
*/
public void manage() {
Evt ev = null;
if(Trigger.isInsert && Trigger.isBefore){
ev = Evt.beforeinsert;
} else if(Trigger.isInsert && Trigger.isAfter){
ev = Evt.afterinsert;
} else if(Trigger.isUpdate && Trigger.isBefore){
ev = Evt.beforeupdate;
} else if(Trigger.isUpdate && Trigger.isAfter){
ev = Evt.afterupdate;
} else if(Trigger.isDelete && Trigger.isBefore){
ev = Evt.beforedelete;
} else if(Trigger.isDelete && Trigger.isAfter){
ev = Evt.afterdelete;
} else if(Trigger.isundelete){
ev = Evt.afterundelete;
}
List<Handler> handlers = eventHandlerMapping.get(ev.name());
if (handlers != null && !handlers.isEmpty()) {
for (Handler h : handlers) {
h.handle();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment