Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How Trigger.new different from Trigger.newmap?

Asked by Deirdre Cameron Jan 2, 2020 4.3K views 2 answers
Share

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on May 16, 2024

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.

Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.