Tuesday, October 31, 2017

Difference between Trigger.new & Trigger.old

Trigger.new : Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.

Trigger.old : Returns a list of the old versions of the sObject records. Note that this sObject list is only available in update and delete triggers.


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 always be null because in before context records is not submitted to database so Id is not generated 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 since id is already generated in Insert event.
So we can use Trigger.newMap in before and After update

No comments:

Post a Comment

Lightning Inter-Component Communication Patterns

Lightning Inter-Component Communication Patterns If you’re comfortable with how a Lightning Component works and want to build producti...