2

I am really confused between these two.i knows the theoretical differences between these like if u have id then only you can use trigger.newmap and trigger.newmap returns a unordered list..

Leaving before insert we can use trigger.new and trigger.newmap both in after insert, after update and before update...

So pls clear my doubt, that in which scenarios i should use trigger.new/trigger.newmap... And its my request to you all dont Copy-paste the standard diffinitions...

  • 1
    One is a list, the other one is a map. I think your question should be "What is the difference between a list and a map" instead. – Boris Bachovski Jan 6 '16 at 6:28
11

Suppose you have a custom object Custom_obj__c

Trigger.New means it is a List<Custom_obj__c>

and

Trigger.NewMap means it is a map<Id, Custom_obj__c>


In before insert context your Trigger.NewMap will always be null because in before context records are not submitted to the database, so the Id is not generated yet. That's why in before insert we don't use Trigger.NewMap But in After insert, Id is generated so we can use Trigger.NewMap

In case of before and after update, the Id has already been generated in the insert event. So we can use Trigger.NewMap in before and after update.

Let me know if you have any confusion..

  • Trigger.oldMap is available in after update or Insert ? – NITHESH K May 22 '18 at 17:09
6

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.

4

Let's take following example.

Suppose you want to get all contact of account object while updating account records and you want to update all contact as well at the same time then you have following option

  1. Loop over the Trigger.New and get all the account ids

    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];
    

Problem with this approach is that if there are 200 account this code will run 200 times because we are using Trigger.new which is list records

  1. Get All the Account ids from Trigger.Newmap

    List<Contact> lstContact = [select id from contact where 
    Accountid in : Trigger.newmap.Keyset()];
    

Above statement will run only 1 time which is always best option to choose from performance perspective.

Conclusion, it all depends what is your requirement. sometime we use Trigger.new and sometime we use Trigger.newMap

2

Trigger.new is a list of sobjects. If we are talking about a DML operation on account, then trigger.new is simply a list of accounts. Similarly when used in contact trigger, trigger.new becomes a list of contacts.

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.

Suppose you are in a classroom, and the teacher wants to take the viva examination of every student. Then she can either call each and every student from the list of students she has( similar to processing records from trigger.new) or call them up one by one by roll number( similar to processing by trigger.newMap where recordID is taken to be equivalent to student roll number).

In case of salesforce functionality, please note that we need the ID of records for a trigger.newMap not for trigger.new. So trigger.newMap dosent exist before the records get their ID( i.e. before insert)

Hope this helps.

1

Trigger.new and Trigger.old both are context variables in Salesforce.

Trigger.New: Trigger.new returns List of new records which are trying to insert into Database. This is available in Before Insert, Before Update, After Insert, After Update Triggers and undelete Triggers. This list of records can only modified in Before triggers.

Trigger.Old: Trigger.old returns List of old records which are updated with new values. These List of records already there in Database. Trigger.old available in Before update, after update, Before Delete and After Delete triggers.

Before Insert Trigger

Trigger.new- what are the records going to insert into Database [not commited yet, id=null] trigger.old- NULL Trigger.newmap-null Trigger.old-Null

Afeter Insert

Trigger.new- New List of Records which are inserted Trigger.old- null Trigger.NewMap- New Map Trigger.oldMap- Null Follow this link for More about Difference Between Trigger.new & Trigger.old

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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