+ Start a Discussion

Calculating Date Delta Across 2 Records In Chronological Order


I am trying to calculate the delta between two date fields on 2 different records. The list of records is sorted in in order by the date field, and i would like the currentRecord, to compare date fields to the previous record before it. Something like:

List of Cases specific to a custom Object:
Case1 CreatedDate Delta
Case2 CreatedDate Delta
Case3 CreatedDate Delta

If Case3 is being looked at in the trigger, i want to be able to compute the delta between 2 and 3, and 2 and 1 and so on, since they are in that chronological order.
I thought this may be possible iterating through a sorted list (in the more detailed use case I have I am sorting the list by 2 fields), and looking at the current list index, and then comparing it to the next index, but it doesnt seem you can check within a loop what index you are currently on.

Anyone else run into this sort of issue and have a solution?