Skip to content

Commit ee05fd3

Browse files
authored
Refactor trigger variable names (#173)
* Refactor trigger variable names * Update package version
1 parent 33fade6 commit ee05fd3

14 files changed

+341
-318
lines changed

README.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Apex Trigger Actions Framework
22

3-
#### [Unlocked Package Installation (Production)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04tKY000000PdZJYA0)
3+
#### [Unlocked Package Installation (Production)](https://login.salesforce.com/packaging/installPackage.apexp?p0=04tKY000000PdZOYA0)
44

5-
#### [Unlocked Package Installation (Sandbox)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04tKY000000PdZJYA0)
5+
#### [Unlocked Package Installation (Sandbox)](https://test.salesforce.com/packaging/installPackage.apexp?p0=04tKY000000PdZOYA0)
66

77
---
88

@@ -68,8 +68,8 @@ public class TA_Opportunity_StageInsertRules implements TriggerAction.BeforeInse
6868
@TestVisible
6969
private static final String INVALID_STAGE_INSERT_ERROR = 'The Stage must be \'Prospecting\' when an Opportunity is created';
7070

71-
public void beforeInsert(List<Opportunity> newList){
72-
for (Opportunity opp : newList) {
71+
public void beforeInsert(List<Opportunity> triggerNew){
72+
for (Opportunity opp : triggerNew) {
7373
if (opp.StageName != PROSPECTING) {
7474
opp.addError(INVALID_STAGE_INSERT_ERROR);
7575
}
@@ -196,10 +196,10 @@ Use the `TriggerBase.idToNumberOfTimesSeenBeforeUpdate` and `TriggerBase.idToNum
196196
```java
197197
public class TA_Opportunity_RecalculateCategory implements TriggerAction.AfterUpdate {
198198

199-
public void afterUpdate(List<Opportunity> newList, List<Opportunity> oldList) {
200-
Map<Id,Opportunity> oldMap = new Map<Id,Opportunity>(oldList);
199+
public void afterUpdate(List<Opportunity> triggerNew, List<Opportunity> triggerOld) {
200+
Map<Id,Opportunity> oldMap = new Map<Id,Opportunity>(triggerOld);
201201
List<Opportunity> oppsToBeUpdated = new List<Opportunity>();
202-
for (Opportunity opp : newList) {
202+
for (Opportunity opp : triggerNew) {
203203
if (
204204
TriggerBase.idToNumberOfTimesSeenAfterUpdate.get(opp.id) == 1 &&
205205
opp.StageName != oldMap.get(opp.id).StageName
@@ -315,17 +315,17 @@ public class TA_Opportunity_Queries {
315315
public Map<Id, Account> beforeAccountMap { get; private set; }
316316

317317
public class Service implements TriggerAction.BeforeInsert {
318-
public void beforeInsert(List<Opportunity> newList) {
318+
public void beforeInsert(List<Opportunity> triggerNew) {
319319
TA_Opportunity_Queries.getInstance().beforeAccountMap = getAccountMapFromOpportunities(
320-
newList
320+
triggerNew
321321
);
322322
}
323323

324324
private Map<Id, Account> getAccountMapFromOpportunities(
325-
List<Opportunity> newList
325+
List<Opportunity> triggerNew
326326
) {
327327
Set<Id> accountIds = new Set<Id>();
328-
for (Opportunity myOpp : newList) {
328+
for (Opportunity myOpp : triggerNew) {
329329
accountIds.add(myOpp.AccountId);
330330
}
331331
return new Map<Id, Account>(
@@ -344,10 +344,10 @@ With the `TA_Opportunity_Queries` class configured as the first action, all subs
344344

345345
```java
346346
public class TA_Opportunity_StandardizeName implements TriggerAction.BeforeInsert {
347-
public void beforeInsert(List<Opportunity> newList) {
347+
public void beforeInsert(List<Opportunity> triggerNew) {
348348
Map<Id, Account> accountIdToAccount = TA_Opportunity_Queries.getInstance()
349349
.beforeAccountMap;
350-
for (Opportunity myOpp : newList) {
350+
for (Opportunity myOpp : triggerNew) {
351351
String accountName = accountIdToAccount.get(myOpp.AccountId)?.Name;
352352
myOpp.Name = accountName != null
353353
? accountName + ' | ' + myOpp.Name
@@ -366,12 +366,12 @@ In the example above, the top-level class is the implementation of the Singleton
366366

367367
## Use of Trigger Maps
368368

369-
To avoid having to downcast from `Map<Id,sObject>`, we simply construct a new map out of our `newList` and `oldList` variables:
369+
To avoid having to downcast from `Map<Id,sObject>`, we simply construct a new map out of our `triggerNew` and `triggerOld` variables:
370370

371371
```java
372-
public void beforeUpdate(List<Opportunity> newList, List<Opportunity> oldList) {
373-
Map<Id,Opportunity> newMap = new Map<Id,Opportunity>(newList);
374-
Map<Id,Opportunity> oldMap = new Map<Id,Opportunity>(oldList);
372+
public void beforeUpdate(List<Opportunity> triggerNew, List<Opportunity> triggerOld) {
373+
Map<Id,Opportunity> newMap = new Map<Id,Opportunity>(triggerNew);
374+
Map<Id,Opportunity> oldMap = new Map<Id,Opportunity>(triggerOld);
375375
...
376376
}
377377
```
@@ -405,38 +405,38 @@ Take a look at how both of these are used in the `TA_Opportunity_StageChangeRule
405405
```java
406406
@IsTest
407407
private static void invalidStageChangeShouldPreventSave() {
408-
List<Opportunity> newList = new List<Opportunity>();
409-
List<Opportunity> oldList = new List<Opportunity>();
408+
List<Opportunity> triggerNew = new List<Opportunity>();
409+
List<Opportunity> triggerOld = new List<Opportunity>();
410410
//generate fake Id
411411
Id myRecordId = TriggerTestUtility.getFakeId(Opportunity.SObjectType);
412-
newList.add(
412+
triggerNew.add(
413413
new Opportunity(
414414
Id = myRecordId,
415415
StageName = Constants.OPPORTUNITY_STAGENAME_CLOSED_WON
416416
)
417417
);
418-
oldList.add(
418+
triggerOld.add(
419419
new Opportunity(
420420
Id = myRecordId,
421421
StageName = Constants.OPPORTUNITY_STAGENAME_QUALIFICATION
422422
)
423423
);
424424

425-
new TA_Opportunity_StageChangeRules().beforeUpdate(newList, oldList);
425+
new TA_Opportunity_StageChangeRules().beforeUpdate(triggerNew, triggerOld);
426426

427427
//Use getErrors() SObject method to get errors from addError without performing DML
428428
System.assertEquals(
429429
true,
430-
newList[0].hasErrors(),
430+
triggerNew[0].hasErrors(),
431431
'The record should have errors'
432432
);
433433
System.assertEquals(
434434
1,
435-
newList[0].getErrors().size(),
435+
triggerNew[0].getErrors().size(),
436436
'There should be exactly one error'
437437
);
438438
System.assertEquals(
439-
newList[0].getErrors()[0].getMessage(),
439+
triggerNew[0].getErrors()[0].getMessage(),
440440
String.format(
441441
TA_Opportunity_StageChangeRules.INVALID_STAGE_CHANGE_ERROR,
442442
new List<String>{
@@ -499,12 +499,12 @@ Finally, use the static variables/methods of the finalizer within your trigger a
499499
public with sharing class TA_Opportunity_RecalculateCategory implements TriggerAction.AfterUpdate {
500500

501501
public void afterUpdate(
502-
List<Opportunity> newList,
503-
List<Opportunity> oldList
502+
List<Opportunity> triggerNew,
503+
List<Opportunity> triggerOld
504504
) {
505-
Map<Id, Opportunity> oldMap = new Map<Id, Opportunity>(oldList);
505+
Map<Id, Opportunity> oldMap = new Map<Id, Opportunity>(triggerOld);
506506
List<Opportunity> toRecalculate = new List<Opportunity>();
507-
for (Opportunity opp : newList) {
507+
for (Opportunity opp : triggerNew) {
508508
if (opp.Amount != oldMap.get(opp.Id).Amount) {
509509
toRecalculate.add(opp);
510510
}

docs/trigger-actions-framework/MetadataTriggerHandler.md

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -183,135 +183,135 @@ public static void clearAllBypasses()
183183

184184
---
185185

186-
### `beforeInsert(newList)`
186+
### `beforeInsert(triggerNew)`
187187

188188
Execute the Before Insert Trigger Actions.
189189

190190
#### Signature
191191
```apex
192-
public void beforeInsert(List<SObject> newList)
192+
public void beforeInsert(List<SObject> triggerNew)
193193
```
194194

195195
#### Parameters
196196
| Name | Type | Description |
197197
|------|------|-------------|
198-
| newList | List&lt;SObject&gt; | The list of new records being inserted. |
198+
| triggerNew | List&lt;SObject&gt; | The list of new records being inserted. |
199199

200200
#### Return Type
201201
**void**
202202

203203
---
204204

205-
### `afterInsert(newList)`
205+
### `afterInsert(triggerNew)`
206206

207207
Execute the After Insert Trigger Actions.
208208

209209
#### Signature
210210
```apex
211-
public void afterInsert(List<SObject> newList)
211+
public void afterInsert(List<SObject> triggerNew)
212212
```
213213

214214
#### Parameters
215215
| Name | Type | Description |
216216
|------|------|-------------|
217-
| newList | List&lt;SObject&gt; | The list of new records that were inserted. |
217+
| triggerNew | List&lt;SObject&gt; | The list of new records that were inserted. |
218218

219219
#### Return Type
220220
**void**
221221

222222
---
223223

224-
### `beforeUpdate(newList, oldList)`
224+
### `beforeUpdate(triggerNew, triggerOld)`
225225

226226
Execute the Before Update Trigger Actions.
227227

228228
#### Signature
229229
```apex
230-
public void beforeUpdate(List<SObject> newList, List<SObject> oldList)
230+
public void beforeUpdate(List<SObject> triggerNew, List<SObject> triggerOld)
231231
```
232232

233233
#### Parameters
234234
| Name | Type | Description |
235235
|------|------|-------------|
236-
| newList | List&lt;SObject&gt; | The list of updated records. |
237-
| oldList | List&lt;SObject&gt; | The list of old records before the update. |
236+
| triggerNew | List&lt;SObject&gt; | The list of updated records. |
237+
| triggerOld | List&lt;SObject&gt; | The list of old records before the update. |
238238

239239
#### Return Type
240240
**void**
241241

242242
---
243243

244-
### `afterUpdate(newList, oldList)`
244+
### `afterUpdate(triggerNew, triggerOld)`
245245

246246
Execute the After Update Trigger Actions.
247247

248248
#### Signature
249249
```apex
250-
public void afterUpdate(List<SObject> newList, List<SObject> oldList)
250+
public void afterUpdate(List<SObject> triggerNew, List<SObject> triggerOld)
251251
```
252252

253253
#### Parameters
254254
| Name | Type | Description |
255255
|------|------|-------------|
256-
| newList | List&lt;SObject&gt; | The list of updated records. |
257-
| oldList | List&lt;SObject&gt; | The list of old records before the update. |
256+
| triggerNew | List&lt;SObject&gt; | The list of updated records. |
257+
| triggerOld | List&lt;SObject&gt; | The list of old records before the update. |
258258

259259
#### Return Type
260260
**void**
261261

262262
---
263263

264-
### `beforeDelete(oldList)`
264+
### `beforeDelete(triggerOld)`
265265

266266
Execute the Before Delete Trigger Actions.
267267

268268
#### Signature
269269
```apex
270-
public void beforeDelete(List<SObject> oldList)
270+
public void beforeDelete(List<SObject> triggerOld)
271271
```
272272

273273
#### Parameters
274274
| Name | Type | Description |
275275
|------|------|-------------|
276-
| oldList | List&lt;SObject&gt; | The list of records being deleted. |
276+
| triggerOld | List&lt;SObject&gt; | The list of records being deleted. |
277277

278278
#### Return Type
279279
**void**
280280

281281
---
282282

283-
### `afterDelete(oldList)`
283+
### `afterDelete(triggerOld)`
284284

285285
Execute the After Delete Trigger Actions.
286286

287287
#### Signature
288288
```apex
289-
public void afterDelete(List<SObject> oldList)
289+
public void afterDelete(List<SObject> triggerOld)
290290
```
291291

292292
#### Parameters
293293
| Name | Type | Description |
294294
|------|------|-------------|
295-
| oldList | List&lt;SObject&gt; | The list of records that were deleted. |
295+
| triggerOld | List&lt;SObject&gt; | The list of records that were deleted. |
296296

297297
#### Return Type
298298
**void**
299299

300300
---
301301

302-
### `afterUndelete(newList)`
302+
### `afterUndelete(triggerNew)`
303303

304304
Execute the After Undelete Trigger Actions.
305305

306306
#### Signature
307307
```apex
308-
public void afterUndelete(List<SObject> newList)
308+
public void afterUndelete(List<SObject> triggerNew)
309309
```
310310

311311
#### Parameters
312312
| Name | Type | Description |
313313
|------|------|-------------|
314-
| newList | List&lt;SObject&gt; | The list of records that were undeleted. |
314+
| triggerNew | List&lt;SObject&gt; | The list of records that were undeleted. |
315315

316316
#### Return Type
317317
**void**

0 commit comments

Comments
 (0)