function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Always ThinkinAlways Thinkin 

Trigger.new vs. Trigger.newMap

What is difference between Trigger.new and Trigger.newMap? The Apex Guide doesn't quite elaborate enough to make it clear.

Thanks,
Luke
Best Answer chosen by Admin (Salesforce Developers) 
garybgaryb

According to the docs, Trigger.new returns a list, which are ordered, and Trigger.newMap returns a map - which are unordered. The docs specifically state you should not rely on the ordering of a map's elements.

 

What to use depends on what you're doing - if you have an ID of an object you need to do something with, using the map makes more sense as you can use newMap.get(). Otherwise you'd have to loop over all the elements in Trigger.new and look for a matching ID. Similarly, if you have multiple loops over each item the trigger is operating on, the list returned by Trigger.new may be the better bet.

All Answers

TehNrdTehNrd
Here is one example of how they can be used, maybe that will help. You can use them to compare old values and new values.

Code:
for(Opportunity opp : Trigger.new){
                    //Create an old and new map so that we can compare values
                    Opportunity oldOpp = Trigger.oldMap.get(opp.ID);    
                    Opportunity newOpp = Trigger.newMap.get(opp.ID);
                    
                    //Retrieve the old and new Reseller Email Field            
                    string oldResellerEmail = oldOpp.Reseller_Email__c;
                    string newResellerEmail = newOpp.Reseller_Email__c;
                   
                    //If the fields are different, the email has changed
                    if(oldResellerEmail != newResellerEmail){
                        oppIDs.put(opp.Id,opp.Reseller_Email__c);    
                    }
}



Always ThinkinAlways Thinkin
What is the advantage of using Trigger.newMap over just using Trigger.new? I have not been using newMap/oldMap and I'm wondering what I'm missing.  For example, my code to compare old and new values in a trigger is:


Code:
trigger OppTrigger on Opportunity (after update) {
 for (Integer i = 0; i < Trigger.old.size(); i++) {
  Opportunity old = Trigger.old[i];
  Opportunity nw = Trigger.new[i];

if ((nw.StageName == 'Closed Won') && (old.StageName != nw.StageName)) {

How does my code limit me and in what scenarios is my code appropriate versus those scenarios where your use of maps is appropriate?

TehNrdTehNrd
I thought I read somewhere in the documentation that trigger.new and trigger.old are unordered and may not always be in the same order. When you use trigger.newMap you can be for certain you are comparing similar records.

This may just be a best practice.
garybgaryb

According to the docs, Trigger.new returns a list, which are ordered, and Trigger.newMap returns a map - which are unordered. The docs specifically state you should not rely on the ordering of a map's elements.

 

What to use depends on what you're doing - if you have an ID of an object you need to do something with, using the map makes more sense as you can use newMap.get(). Otherwise you'd have to loop over all the elements in Trigger.new and look for a matching ID. Similarly, if you have multiple loops over each item the trigger is operating on, the list returned by Trigger.new may be the better bet.

This was selected as the best answer
TehNrdTehNrd
Ahh, thanks for clarifying.
Message Edited by TehNrd on 03-24-2010 08:22 AM