A Tip A Day #37 – Trigger Helper Class

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);
   }
}

 


Read all other tips of the blog series here – A Tip A Day, daily dosage of learning!


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Advertisement

3 Replies to “A Tip A Day #37 – Trigger Helper Class”

    1. 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

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: