This post is a part of the daily blog series
A Tip A Day, daily dosage of learning!
Day #37 – Trigger Helper Class
I have a trigger that calls an Apex Handler class that does all the functionality of the trigger.
The handler class uses static methods. In our scenario, the handler method calls the static method “updateFields” within its class by passing trigger.new and trigger.oldMap values to the method.
The issue is the trigger.OldMap errors out saying method doesn’t exist!
public handler(){ for(Contact c:trigger.new){ //Using trigger.New and Trigger.OldMap //FAIL. Cannot use trigger.OldMap Directly updateFields(trigger.new,Trigger.OldMap); } } //static method public static void updateFields(List<Contact> conList, Map<Id,Contact> MapIdCon){ //processes lists and updates contact fields }
Solution
For trigger.OldMap, we need to cast it to the Map<Id,Contact> again to resolve the issue.
public handler(){ for(Contact c:trigger.new){ //Using trigger.New and Trigger.OldMap //WORKS. Need to Cast before using oldMap updateFields(trigger.new,(Map<Id,Contact>)Trigger.OldMap); } }
Can you be more specific about “errors out”?
I use old maps in triggers on a regular basis without any issues so curious what scenario is causing this to not work for you.
LikeLiked by 1 person
Thanks Steve for checking that. The error is:
Method does not exist or incorrect signature: void updateFields(List, Map) from the type TriggerHandler
Same error even if I change the trigger.oldMap to trigger.old
LikeLike