2

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

1

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):

for each record in trigger.old
  if record.field == 'A' && trigger.newMap.get(record.Id).field == 'C'
    error('You can not change field straight from A to C!');

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.