-
Notifications
You must be signed in to change notification settings - Fork 6
/
TriggerHandler.cls
194 lines (191 loc) · 16.9 KB
/
TriggerHandler.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* ─────────────────────────────────────────────────────────────────────────────────────────────────┐
* This class is a core component of the Simplified Trigger Pattern for Salesforce (STP).
*
* All trigger handler classes that implement the Simple Trigger Pattern must extend this class.
* Please note that under normal circumstances, this class should not be modified.
* ──────────────────────────────────────────────────────────────────────────────────────────────────
* Copyright (c) 2015 Vivek M. Chawla (@VivekMChawla)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
* associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* ──────────────────────────────────────────────────────────────────────────────────────────────────
* @author Vivek M. Chawla <Vivek.M.Chawla@gmail.com>
* @modifiedBy Vivek M. Chawla <Vivek.M.Chawla@gmail.com>
* @maintainedBy Vivek M. Chawla <Vivek.M.Chawla@gmail.com>
* @version 1.1
* @created 2014-03-12
* @modified 2015-04-05
* @systemLayer Invocation
* ──────────────────────────────────────────────────────────────────────────────────────────────────
* @changes
* v1.0 Vivek.M.Chawla@gmail.com
* 2014-03-12 Initial implementation.
*
* v1.1 Vivek.M.Chawla@gmail.com
* 2015-04-05 Added additional comments explaining how the code in this class works. Changed
* the HandlerException class from public to private. Simplified the "recursion
* check" logic and added @testVisible annotations to each one. Migrated from a CC
* license to the MIT license.
* ─────────────────────────────────────────────────────────────────────────────────────────────────┘
*/
public virtual class TriggerHandler {
//───────────────────────────────────────────────────────────────────────────┐
// Initialize static booleans to track if this is the first time a specific
// Trigger Action has been called within the current Execution Context.
//───────────────────────────────────────────────────────────────────────────┘
public static boolean isBeforeInsertFirstRun = true;
public static boolean isBeforeUpdateFirstRun = true;
public static boolean isBeforeDeleteFirstRun = true;
public static boolean isAfterInsertFirstRun = true;
public static boolean isAfterUpdateFirstRun = true;
public static boolean isAfterDeleteFirstRun = true;
public static boolean isAfterUndeleteFirstRun = true;
//───────────────────────────────────────────────────────────────────────────┐
// If the trigger was invoked by a DML operation on more than 200 records,
// multiple instances of the handler class will be created within the same
// execution context. This attribute tracks the batch size each instance
// works on.
//───────────────────────────────────────────────────────────────────────────┘
protected final integer batchSize;
//───────────────────────────────────────────────────────────────────────────┐
// Declare an inner class that extends Exception. This will allow us to throw
// a custom Exception from within this Handler Class. If presented to a user,
// or inspected in a debug log, this exception will appear as an exception of
// type "TriggerHandler.HandlerException".
//───────────────────────────────────────────────────────────────────────────┘
private class HandlerException extends Exception {}
/**
* ───────────────────────────────────────────────────────────────────────────────────────────────┐
* Constructor
* ───────────────────────────────────────────────────────────────────────────────────────────────┘
*/
public TriggerHandler() {
//─────────────────────────────────────────────────────────────────────────┐
// Ensure that this handler class is being instantiated by a Trigger. If it
// is not, then we must kill execution and throw an Exception. The only
// ...ahem..."exception" to this rule is if this handler class is being
// instantiated by a test method.
//─────────────────────────────────────────────────────────────────────────┘
if (Trigger.isExecuting != true && Test.isRunningTest() == false) {
throw new HandlerException('This class may only be instantiated within a Trigger-based '
+'Execution Context.');
}
//─────────────────────────────────────────────────────────────────────────┐
// Initialize the batchSize instance variable with the value from the
// Trigger.size context variable. This allows us to track the number of
// records in the trigger batch at the time this class was instantiated.
//─────────────────────────────────────────────────────────────────────────┘
batchSize = Trigger.size;
}
/**
* ───────────────────────────────────────────────────────────────────────────────────────────────┐
* Checks if the BEFORE INSERT static flag has been tripped, and trips the flag if it has not.
* ────────────────────────────────────────────────────────────────────────────────────────────────
* @return boolean Returns TRUE if the BEFORE INSERT trigger has already run, FALSE if not.
* ───────────────────────────────────────────────────────────────────────────────────────────────┘
*/
@testVisible
protected boolean beforeInsertHasRun() {
if (isBeforeInsertFirstRun) {
return isBeforeInsertFirstRun = false;
}
return true;
}
/**
* ───────────────────────────────────────────────────────────────────────────────────────────────┐
* Checks if the BEFORE UPDATE static flag has been tripped, and trips the flag if it has not.
* ────────────────────────────────────────────────────────────────────────────────────────────────
* @return boolean Returns TRUE if the BEFORE UPDATE trigger has already run, FALSE if not.
* ───────────────────────────────────────────────────────────────────────────────────────────────┘
*/
@testVisible
protected boolean beforeUpdateHasRun() {
if (isBeforeUpdateFirstRun) {
return isBeforeUpdateFirstRun = false;
}
return true;
}
/**
* ───────────────────────────────────────────────────────────────────────────────────────────────┐
* Checks if the BEFORE DELETE static flag has been tripped, and trips the flag if it has not.
* ────────────────────────────────────────────────────────────────────────────────────────────────
* @return boolean Returns TRUE if the BEFORE DELETE trigger has already run, FALSE if not.
* ───────────────────────────────────────────────────────────────────────────────────────────────┘
*/
@testVisible
protected boolean beforeDeleteHasRun() {
if (isBeforeDeleteFirstRun) {
return isBeforeDeleteFirstRun = false;
}
return true;
}
/**
* ───────────────────────────────────────────────────────────────────────────────────────────────┐
* Checks if the AFTER INSERT static flag has been tripped, and trips the flag if it has not.
* ────────────────────────────────────────────────────────────────────────────────────────────────
* @return boolean Returns TRUE if the AFTER INSERT trigger has already run, FALSE if not.
* ───────────────────────────────────────────────────────────────────────────────────────────────┘
*/
@testVisible
protected boolean afterInsertHasRun() {
if (isAfterInsertFirstRun) {
return isAfterInsertFirstRun = false;
}
return true;
}
/**
* ───────────────────────────────────────────────────────────────────────────────────────────────┐
* Checks if the AFTER UPDATE static flag has been tripped, and trips the flag if it has not.
* ────────────────────────────────────────────────────────────────────────────────────────────────
* @return boolean Returns TRUE if the AFTER UPDATE trigger has already run, FALSE if not.
* ───────────────────────────────────────────────────────────────────────────────────────────────┘
*/
@testVisible
protected boolean afterUpdateHasRun() {
if (isAfterUpdateFirstRun) {
return isAfterUpdateFirstRun = false;
}
return true;
}
/**
* ───────────────────────────────────────────────────────────────────────────────────────────────┐
* Checks if the AFTER DELETE static flag has been tripped, and trips the flag if it has not.
* ────────────────────────────────────────────────────────────────────────────────────────────────
* @return boolean Returns TRUE if the AFTER DELETE trigger has already run, FALSE if not.
* ───────────────────────────────────────────────────────────────────────────────────────────────┘
*/
@testVisible
protected boolean afterDeleteHasRun() {
if (isAfterDeleteFirstRun) {
return isAfterDeleteFirstRun = false;
}
return true;
}
/**
* ───────────────────────────────────────────────────────────────────────────────────────────────┐
* Checks if the AFTER UNDELETE static flag has been tripped, and trips the flag if it has not.
* ────────────────────────────────────────────────────────────────────────────────────────────────
* @return boolean Returns TRUE if the AFTER UNDELETE trigger has already run, FALSE if not.
* ───────────────────────────────────────────────────────────────────────────────────────────────┘
*/
@testVisible
protected boolean afterUndeleteHasRun() {
if (isAfterUndeleteFirstRun) {
return isAfterUndeleteFirstRun = false;
}
return true;
}
}