18 Jun 2018
3 MINS READ
Salesforce’s Automation tools offers some non-declarative ways to customize. You can achieve quite a lot with those automation tools which are based on Salesforce’s philosophy of “With Click, Not Code”. For complex customization needs, those tools hit their limitation and development of code becomes essential. Apex Triggers are one of the essential features in achieving complex customization. Over a period of time, a simple trigger can become very complicated as platform gets further customized with changing requirements.
Apex Trigger Frameworks provides a structure that allows easy maintenance and enforcement of best practices. Implementing Trigger Framework can reduce your development lifecycle. One of the best practices is to use a single trigger per object. It reduces clutter and provides easy maintenance and control. In a larger team environment, it provides single code base to work on making it a lot easier to write test classes.
Another best practice is to make your Triggers logic-less. The role of the Trigger is just to delegate the logic responsibilities to some other handler class, in order to avoid future mess.
We will look at two popular trigger frameworks that will help you maintain the best practices and also make good use of the defined structure they provide.
Framework 1: Tony Scot’s Trigger Patterns – Trigger Pattern for Tidy, Streamlined, Bulkified Triggers
This framework utilizes an interface, Trigger Handler class and a factory class
Benefits of this framework are as follows:
Here, we will look at bits of code to understand how framework operates.
Your trigger will look like a one line of code, as given below:
TriggerFactory class is used to instantiate Trigger Handler associated with a particular sObject. TriggerFactory class’ execute method is called, and in turn call to handler’s methods will happen at appropriate trigger events.
Handler class will need to implement interface ITrigger as shown in below line.
public with sharing class AccountHandler implements ITrigger
ITrigger interface makes it mandatory to implement certain methods and these methods provide a structured way to write trigger code. For example, bulkBefore method is called prior to execution of BEFORE trigger and can be utilized to cache any data into maps. Method beforeInsert is called iteratively for each record to be inserted during a BEFORE trigger, it is recommended not to execute SOQL in this method.
Below is the list of methods that structures the code within handler.
Methods that are meant for caching data before processing:
Methods that gets called iteratively in trigger loop:
Below method is called once all records have been processed by the trigger. Use this method to accomplish any final operations such as creation or updates of other records.
Framework 2: Kevin oHara’s SFDC Trigger Framework
This framework provides a base class called TriggerHandler which includes context-specific methods that are automatically called when a trigger is executed.
Below is an example of Account Trigger Handler
You will need to override methods in your handler for which you want trigger to execute custom logic.
Below is sample code in Account Trigger handler which overrides method beforeUpdate and will have code to handle trigger logic.
Here are all the methods that you can override in handler:
To call the trigger handler from trigger, all you need is to construct an instance of trigger handler and call the run() method. Here is an example of the Opportunity trigger.
Kevin oHara’s SFDC Trigger Framework is easy to implement. Tony Scot’s Trigger Patterns provides separation of logic for bulkification with methods bulkBefore(), and bulkAfter(). Both trigger frameworks make writing and maintaining triggers easy and also ensure alignment of with best practices.
Niraj has extensive experience in Enterprise CRM applications, solution design, architecture and system integrations. 3x Salesforce Certified Professional, AWS Certified Cloud Practitioner and playing role of an application architect in his current assignment.
(nirajw@hexaware.com)
For more insights please feel free to connect with us on marketing@hexaware.com.
About the Author
Customer Experience Transformation, Healthcare
30 Mar 2020
Customer Experience Transformation
26 Jul 2019
18 Aug 2018
28 Jun 2018
BI & Analytics
19 Mar 2009
26 Nov 2010
Digital Assurance
02 Jan 2012
17 Feb 2012
Infrastructure Mgmt. Services
02 Mar 2012
03 Jan 2013
04 Feb 2013
06 Feb 2013
Digital Assurance, Enterprise Solutions
14 Feb 2013
18 Feb 2013
21 Feb 2013
27 Feb 2013
Others
01 Mar 2013
04 Mar 2013
Enterprise Solutions
05 Mar 2013
18 Mar 2013
Digital Assurance, Enterprise Solutions, Others
22 Mar 2013
12 Apr 2013
26 Apr 2013
29 Apr 2013
13 May 2013
11 Jun 2013
17 Jun 2013
25 Jun 2013
19 Aug 2013
26 Aug 2013
27 Aug 2013
03 Sep 2013
10 Sep 2013
19 Sep 2013
24 Sep 2013
26 Sep 2013
30 Sep 2013
01 Oct 2013
03 Oct 2013
01 Nov 2013
19 Nov 2013
Enterprise Solutions, Manufacturing and Consumer
28 Nov 2013
03 Dec 2013
20 Dec 2013
03 Jan 2014
22 Jan 2014
27 Jan 2014
31 Jan 2014
12 Feb 2014
13 Feb 2014
20 Mar 2014
24 Mar 2014
17 Apr 2014
11 Jun 2014
Manufacturing and Consumer
26 Jun 2014
30 Jun 2014
10 Jul 2014
15 Jul 2014
16 Jul 2014
18 Jul 2014
28 Oct 2014
13 Jul 2015
06 Aug 2015
26 Aug 2015
28 Sep 2015
07 Oct 2015
26 Oct 2015
07 Mar 2016
22 Mar 2016
13 May 2016
23 May 2016
Application Transformation Mgmt.
11 Jul 2016
25 Aug 2016
03 Sep 2016
14 Sep 2016
15 Nov 2016
22 Nov 2016
25 Nov 2016
Business Process Services
25 Apr 2017
Banking and Financial Services
18 May 2017
30 May 2017
23 Jun 2017
27 Jun 2017
18 Jul 2017
26 Oct 2017
Healthcare, Insurance
28 Nov 2017
11 Dec 2017
25 Jan 2018
21 Feb 2018
( Mandatory field * )
The information you provide will be used in accordance with our terms ofPrivacy Policy
Please Check on "I Agree" to register for the blog.