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
197197public 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
346346public 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
407407private 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
499499public 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 }
0 commit comments