+ Start a Discussion
Yogesh BiyaniYogesh Biyani 

replacement for System.Trigger.oldMap.get(oIterator.Id).CloseDate in a class

Currently, we have 8 different triggers on the opportunity class and I am refactoring the code and moving it to a helper class. I am thinking of using the switch statement as shown in this article (https://developer.salesforce.com/blogs/2018/05/summer18-rethink-trigger-logic-with-apex-switch.html). 

Here is the original trigger
 trigger OppPusher on Opportunity (before update) {
Date dNewCloseDate;
Date dOldCloseDate;
Boolean bPushed=false;
for (Opportunity oIterator : Trigger.new) { //Bulk trigger handler so that you can mass update opportunities and this fires for all'
// gets new values for updated rows
dNewCloseDate = oIterator.CloseDate; // get the new closedate
dOldCloseDate = System.Trigger.oldMap.get(oIterator.Id).CloseDate; //get the old closedate for this opportunity
//if different do something.
}
}

Here is the new trigger and class 
trigger OppTriggers  on  Opportunity (    before insert, 
                                           after insert, 
                                           before update, 
                                           after update) {

	OppTriggerHandler.handleTrigger(Trigger.new, Trigger.old, Trigger.operationType);

}

public with sharing class OppTriggerHandler { 
    
    public static void handleTrigger(List<Opportunity> workingRecords, 
                                     List<Opportunity> oldRecords, 
                                     System.TriggerOperation triggerEvent ) {
                                         
                                         
                                         switch on triggerEvent {
                                             
                                             when BEFORE_UPDATE{
                                                 Date dNewCloseDate;
                                                 Date dOldCloseDate;
                                                 Boolean bPushed=false;
                                                 
                                                 for (Opportunity oIterator : workingRecords) { //Bulk trigger handler so that you can mass update opportunities and this fires for all'
                                                     // gets new values for updated rows
                                                     dNewCloseDate = oIterator.CloseDate; // get the new closedate 
                                              	     dOldCloseDate = System.Trigger.oldMap.get(oIterator.Id).CloseDate; //get the old closedate for this opportunity
                                                    //if different do something.
                                                   
                                                 }
                                             }
                                             when AFTER_INSERT, AFTER_UPDATE {
                                                 //create related records
                                             }
                                             when BEFORE_INSERT {
                                                 //set value on record create
                                             }
                                             when AFTER_DELETE {
                                                 //prevent deletion of sensitive data
                                             }
                                             when else {
                                                 //do nothing for AFTER_UNDELETE, BEFORE_DELETE, or BEFORE_UPDATE
                                             }
                                         }
                                     }
}

And the compiler fails with 
Variable does not exist: CloseDate message.

How do I about this error message? 


 
Raj VakatiRaj Vakati
Change your code like below
 
trigger OppTriggers  on  Opportunity (    before insert, 
                                           after insert, 
                                           before update, 
                                           after update) {

	OppTriggerHandler.handleTrigger(Trigger.new, Trigger.oldMap, Trigger.operationType);

}
 
public with sharing class OppTriggerHandler { 
    
    public static void handleTrigger(List<Opportunity> workingRecords, 
                                     Map<Id, Opportunity> oldRecords, 
                                     System.TriggerOperation triggerEvent ) {
                                         
                                         
                                         switch on triggerEvent {
                                             
                                             when BEFORE_UPDATE{
                                                 Date dNewCloseDate;
                                                 Date dOldCloseDate;
                                                 Boolean bPushed=false;
                                                 
                                                 for (Opportunity oIterator : workingRecords) { //Bulk trigger handler so that you can mass update opportunities and this fires for all'
                                                     // gets new values for updated rows
                                                     dNewCloseDate = oIterator.CloseDate; // get the new closedate 
                                              	     dOldCloseDate = oldRecords.get(oIterator.Id).CloseDate; //get the old closedate for this opportunity
                                                    //if different do something.
                                                   
                                                 }
                                             }
                                             when AFTER_INSERT, AFTER_UPDATE {
                                                 //create related records
                                             }
                                             when BEFORE_INSERT {
                                                 //set value on record create
                                             }
                                             when AFTER_DELETE {
                                                 //prevent deletion of sensitive data
                                             }
                                             when else {
                                                 //do nothing for AFTER_UNDELETE, BEFORE_DELETE, or BEFORE_UPDATE
                                             }
                                         }
                                     }
}

 
Yogesh BiyaniYogesh Biyani
Hello Raj,

The compile error is resolved. On to next steps! 

Thanks.

Yogesh
Raj VakatiRaj Vakati
Cool  . CLose this thread 
Yogesh BiyaniYogesh Biyani
How do I close the thread as solved?
Raj VakatiRaj Vakati
Yes