Activity Forums Salesforce® Discussions How and when can we use Trigger.newMap in a Salesforce Validation Trigger?

  • Avnish Yadav

    Member
    August 2, 2018 at 5:50 am

    Hi Prachi,

    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.

    When 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.

    Thanks.

  • Parul

    Member
    September 28, 2018 at 5:22 pm

    Trigger.newMap just allows you to target specific records by Id should you not need to process everything. It returns a map which is unordered. Trigger.newMap is a map represented as map<Id, Custom_obj__c>.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos