Activity › Forums › Salesforce® Discussions › Difference between Trigger.New and Trigger.NewMap in Salesforce?
Tagged: Account ID, Difference, Maps in Salesforce, Salesforce Development, Salesforce sObject, Salesforce Trigger, Trigger New, Trigger NewMap, Trigger OldMap
-
Difference between Trigger.New and Trigger.NewMap in Salesforce?
Posted by sushant on December 8, 2016 at 2:47 PMHi All,
What is the difference between Trigger.New and Trigger.NewMap in Salesforce?
please give suggestions!
Thanks
Parul replied 7 years, 7 months ago 4 Members · 4 Replies -
4 Replies
-
Hi Sushant
Trigger.new return the ordered list whereas trigger.newmap returns a map which is unordered
In other ways
Trigger.new
Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.Trigger.NewMap
A map of IDs to the new versions of the sObject records, this map is only available in before update, after insert, and after update triggers.hope it may help
-
This reply was modified 7 years, 4 months ago by
Forcetalks.
-
This reply was modified 7 years, 4 months ago by
- [adinserter block='9']
-
Hi,
Trigger.OldMap: A map of IDs to the old versions of the sObject records. Note that this map is only available in update and delete triggers.
Trigger.newMap: Trigger.newMap is a map with key as ID of the record and value as the record itself. Just like the above explanation, in case of accounts when we say trigger.newMap we are talking about a map of key-value pairs where the key is the account ID and the value is the account record itself.
A map of IDs to the new versions of the sObject records.Note that this map is only available in before update, after insert, and after update triggers.
oldMap
A map of IDs to the old versions of the sObject records.Note that this map is only available in update and delete triggers.Hope this helps.
-
Hi Shariq, Difference in Trigger.New and Trigger.NewMap:
trigger.new is simply a list of the records being processed by the trigger, so if all you need to do is loop through them then you can use that.
trigger.newMap just allows you to target specific records by Id should you not need to process everything, or if you need to map other records back to these.
set<Id> accIds = new set<Id>();
for(Account acc : Trigger.new)
{
accIds.add(acc.id);
}
//query the contact records now from currently processed Account Ids
List<Contact> lstContact = [select id from contact where Accountid in : accIds];Get All the Account ids from Trigger.Newmap
List<Contact> lstContact = [select id from contact where
Accountid in : Trigger.newmap.Keyset()];Thanks
Log In to reply.