Is the order of records the same in the Trigger.new list in before and after trigger?

Is the order of records in the Trigger.new list the same in before and after trigger?

Can I rely on the assumption that:
on before r1 in the Trigger.new list on the 1st place;
on after r1 in the Trigger.new list on the 1st place;
Thanks

Answer

No, you should not rely on the order of records in Trigger.new and Trigger.old. Use Trigger.newMap and Trigger.oldMap instead.

It’s a better practice to use Trigger.newMap and Trigger.oldMap because you can’t assume that directly querying the Trigger.new and Trigger.old lists will return the same number of records in the same order. Even though these lists are sorted by ID, external operations might change the number of records that are returned and make parallel list processing dangerous.

Source: http://developer.force.com/cookbook/recipe/comparing-queries-against-trigger-old-and-trigger-new

Attribution
Source : Link , Question Author : Natallia , Answer Author : georg w.

Leave a Comment