Difference between trigger.new and trigger.newmap

  • I am really confused between these two.I know the theoretical differences between these like if you have the id then only you can use trigger.newmap and trigger.newmap returns an unordered list...

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

    So please clear my doubt: in which scenarios i should use trigger.new/trigger.newmap?

    And it is my request to you all to not copy-paste the standard definition...

    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.

  • Ratan Paul

    Ratan Paul Correct answer

    6 years ago

    Suppose you have a custom object Custom_obj__c

    Trigger.New means it is a List<Custom_obj__c>


    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 ?

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM