I am new to salesforce and apex and when reading about triggers, I found this confusing.
Could somebody clarify on this please?
From salesforce documentation:
newMap–>A map of IDs to the new versions of the sObject records.
Note that this map is only available in before update, after insert, and after update triggers.
If Trigger.newMap givesAccess to the new version of SObjects why we need to use this in before events?
If i use Trigger.newMap in beforeUpdate will I get old values of the record of new values of the record?
Is my understanding correct?
For the events before Update, after Update we have access to the variables Trigger.old and Trigger.new
Best Answer
New map, as the name suggests, is to provide access to the new values the records will take on during the DML operation.
You might want this in before events to perform some kind of validation, for example (pseudeocode):
Using
newMap
in a Before Update trigger will let you access the new field values,oldMap
will provide you with the existing values. As you can see from the above, it's useful to have access to both so that you can take action as business logic dictates.