I was wondering how to clone trigger.newmap or how to still have access to the references associated to it when I want to pass this into a trigger handler.

  • 2
    What is driving the need to clone it? It's typical to pass trigger context variables directly to your handler methods. (Please edit to specify further) – David Reed Mar 9 '20 at 18:54

If you're looking to clone the map itself, then the following should work for most needs:

Map<Id, Account> = new Map<Id, Account>(Trigger.newmap);

... where Account is the specific type of the trigger. This will create a new map, such that if it is modified by later code, the original map of Trigger.newmap will remain unchanged.

  • 3
    Trigger.newMap.clone() works as well. – sfdcfox Mar 9 '20 at 19:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.