How Trigger.new different from Trigger.newmap?

3.2K    Asked by DeirdreCameron in Salesforce , Asked on Jan 2, 2020
Answered by Deirdre Cameron

Trigger.New :- this is a list of sobjects and also returns a list of records in an ordered manner. In case of DML operation on opportunity, trigger.new will contain all list of opportunity records. In the similar way, if DML operates on a contact object, trigger.new will contain a list of contact records.

Support we have custom object called AccountContactRelationship_c then trigger.New means list

Trigger.newmap:- This is a map with key as Id of salesforce record and value as the record itself. In similar kind of example which we mentioned above, whenever or wherever we will use trigger,newmap then it means key and value pair which mean opportunity Id and opportunity record it self.Also, it’s really important to notice that trigger.newmap can’t be used in before insert operation because record will be not available in before insert. So trigger.newmap will not get any record Id.

Support we have custom object called AccountPartnerTeam_c then trigger.New means map



Your Answer

Answer (1)

What distinguishes Trigger.new from Trigger.newMap in Apex?


Trigger.new and Trigger.newMap are both collections provided by Salesforce Apex to access records that are being inserted, updated, or undeleted in a trigger context. However, they serve different purposes and provide access to the records in different formats:

Trigger.new: It represents a list of records that are being processed by the trigger. This list contains the new versions of records that are being inserted or updated. Each record is represented as an SObject instance. For example, if the trigger is fired by inserting Account records, Trigger.new would contain a list of Account objects.

Trigger.newMap: It represents a map of records keyed by their record IDs. The keys are the IDs of the records, and the values are the new versions of the records. This map is particularly useful when you need to perform operations based on record IDs or need to quickly access specific records within the trigger context.

Here's a brief comparison:

Trigger.new provides a list of records.

Trigger.newMap provides a map of records keyed by their record IDs.

For example, in a before insert trigger on the Account object, you might use Trigger.new to access a list of newly inserted Account records. If you need to quickly find a specific Account within this list based on its ID, you could use Trigger.newMap to create a map of Account records keyed by their IDs and then access the specific Account record using its ID as the key.

Understanding the difference between Trigger.new and Trigger.newMap allows developers to efficiently work with records within trigger contexts and perform necessary operations based on their requirements.

6 Months

Interviews

Parent Categories