15

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

2
  • 2
    Even if they are in the same order though, there is no reason to ever need for them to be.
    – dphil
    Commented May 19, 2014 at 14:14
  • 4
    I think you would be safer using the maps provided.
    – Mike Chale
    Commented May 19, 2014 at 14:24

4 Answers 4

10

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

1
  • 4
    I realize this is old, but it's definitely not correct. Each element in Trigger.old will correspond to the same record in Trigger.new. The documentation quoted here only applies if you choose to perform a query on Trigger.old or Trigger.new values.
    – sfdcfox
    Commented Dec 5, 2016 at 6:50
1

I would say yes since the triggers are associated with the list of records and not as a result of an actual database query. Similar to the database.saveresult where the records are returned in the same order as they are inserted.

I do not have any hard documentation on this but I would bet it is a good assumption..

1

In a trigger records are sorted by Salesforce Id.

So, In case of insert you will get same order throughout the process. Also records insert in order they are inserted in list so first record will get the least id and last one will get greatest Id.

I will not recommend to rely on that order. As it will require a complex design to function properly with different trigger event. However order will be same in after and before operations.

0

In addition to the other valuable remarks by other respondents, records in a before trigger that fail validation rules or otherwise have an addError() method applied to them won't even appear in the after trigger's Trigger.new variable. Thus, the size of the lists may not be equal, before vs after.

4
  • Actually, they will, assuming you don't cache the results. Trigger.old.size will always equal Trigger.new.size.
    – sfdcfox
    Commented Dec 5, 2016 at 6:50
  • @sfdcfox. Records that fail VR rules appear in after Trigger's Trigger.new? I agree that in any Trigger context, Trigger.old size = Trigger.new size assuming both are available
    – cropredy
    Commented Dec 5, 2016 at 7:03
  • When a partial success is allowed, the records that failed half-way through are set aside, and a new Trigger.new and Trigger.old will be created. They should always have the same number of records, and each index in the array should point to the other. There's never a circumstance where Trigger.new.size will differ from Trigger.old.size, nor will index X not represent the same record in Trigger.new and Trigger.old.
    – sfdcfox
    Commented Dec 5, 2016 at 7:07
  • @sfdcfox. Yes, agree. I probably meant to say in the original answer for a partial success use case, that the size of Trigger.new in the before Trigger is not the same size of the Trigger.new in the after Trigger. And specifically, that index i in the after Trigger doesn't point to the same record at index i in the before Trigger in case one were caching in static variables.
    – cropredy
    Commented Dec 5, 2016 at 7:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .