Skip to content

refactored for the sake of flexibility #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
# tscottdev-trigger-framework

I reference this framework from time to time and thought it might be handy to create a repo for [Tony Scott's](https://github.com/tscottdev) 2013 code recipe, [Trigger Pattern for Tidy, Streamlined, Bulkified Triggers](https://developer.secure.force.com/cookbook/recipe/trigger-pattern-for-tidy-streamlined-bulkified-triggers).

Basic usage:

```apex
class ObjectHandler implements ITrigger {
public bulkBefore() {
if (Trigger.isDelete) { /* do some bulk stuff before delete: collect additional data, */ }
}
public bulkAfter() {}
public beforeInsert(SObject so) { /* do some ops with individual records (no SOQL or DML recommended! */ }
public beforeUpdate(SObject oldSo, SObject so) {}
public beforeDelete(SObject so) {}
public afterInsert(SObject so) {}
public afterUpdate(SObject oldSo, SObject so) {}
public afterDelete(SObject so) {}
public andFinally() { /* any post processing */ }
}

trigger ObjectTrigger on SObject (after delete, after insert, after update, before delete, before insert, before update)
{
TriggerFactory.execute(new ObjectHandler());
}
```
2 changes: 1 addition & 1 deletion example/triggers/AccountTrigger.trigger
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
trigger AccountTrigger on Account (after delete, after insert, after update, before delete, before insert, before update)
{
TriggerFactory.createHandler(Account.sObjectType);
TriggerFactory.execute(new AccountHandler());
}
42 changes: 1 addition & 41 deletions src/classes/TriggerFactory.cls
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,12 @@
*/
public with sharing class TriggerFactory
{
/**
* Public static method to create and execute a trigger handler
*
* Arguments: Schema.sObjectType soType - Object type to process (SObject.sObjectType)
*
* Throws a TriggerException if no handler has been coded.
*/
public static void createHandler(Schema.sObjectType soType)
{
// Get a handler appropriate to the object being processed
ITrigger handler = getHandler(soType);

// Make sure we have a handler registered, new handlers must be registered in the getHandler method.
if (handler == null)
{
throw new TriggerException('No Trigger Handler registered for Object Type: ' + soType);
}

// Execute the handler to fulfil the trigger
execute(handler);
}

/**
* private static method to control the execution of the handler
*
* Arguments: ITrigger handler - A Trigger Handler to execute
*/
private static void execute(ITrigger handler)
public static void execute(ITrigger handler)
{
// Before Trigger
if (Trigger.isBefore)
Expand Down Expand Up @@ -99,22 +77,4 @@ public with sharing class TriggerFactory
// Perform any post processing
handler.andFinally();
}

/**
* private static method to get the appropriate handler for the object type.
* Modify this method to add any additional handlers.
*
* Arguments: Schema.sObjectType soType - Object type tolocate (SObject.sObjectType)
*
* Returns: ITrigger - A trigger handler if one exists or null.
*/
private static ITrigger getHandler(Schema.sObjectType soType)
{
if (soType == Account.sObjectType)
{
return new AccountHandler();
}

return null;
}
}