How to reference a previous iteration in forEach in subsequent iterations

Certified Senior Developer

My use case is as follows:

I have a CDT of dates and number of days.  I have the first date, but the others are not populated yet.  My goal is to populate each subsequent date with the previous indexes date + the number of days of the current iteration.

For example I start with the following:

{9/27/2021, 0}, {null,3}, {null,5}

I want the output to be as follows:

{9/27/2021, 0}, {9/30/2021,3}, {10/5/2021,5}

Each iteration will reference the previous iteration's date to generate its own date.

I appreciate your time and help!

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    This sort of case is tougher to handle even in a!forEach() implementations (don't get me started on the horrible older looping functions it replaced).  The issue is that we have to assume every loop of the rule evaluate simultaneously - meaning in evaluation X, we don't have access to the result of evaluation X - 1.  We can, however, look at the original value of X - 1 (i.e. "local!myArray[fv!index - 1]"), which only helps us partially in your case.

    So to handle item 2 in your array it's fairly easy -- something like resultDate: local!myArray[fv!index - 1] + fv!item.days

    The issue is, for item 3 in your array, it'll look back at the original value of item 2 (with a blank date) and that won't be much help.  I think the solution here would be to build a sub-loop inside the primary loop, which looks back to the beginning of the list and finds the first non-null date (and of course it would need to track all the differential "days" values in between, to further complicate matters).  If I have time later I might try circling back to this to see if I can help with a working example, unless someone beats me to it, but I don't know it off the top of my head (and i can't 100% guarantee it's possible, though I suspect it is).  It may be the sort of thing that gets far easier if you write a recursive helper rule, though that always gets tricky to implement and test.

  • 0
    Certified Senior Developer
    in reply to Mike Schmitt

    Thank you for your reply, Mike!  I'm still tinkering and actually came up with a really simple solution. I'm going to test it but essentially what I did is as follows:
    Since I know the first date, and all subsequent dates are built off of the first date then I am using the following function in a!forEach - firstDate + sum(index(arrayOfNumberOfDays,enumerate(fv!index) + 1,{}))

    What this does is it takes our firstDate then adds the number of days for each iteration, combining previous number of days for all subsequent iterations.  Using my example above the second iteration will add 3 days, the third iteration will add 8 days.

    For now I think I've found a viable solution, but I will be keeping this post open a little while longer because I would love to see the inputs that others have.  Definitely an interesting problem!

Reply
  • 0
    Certified Senior Developer
    in reply to Mike Schmitt

    Thank you for your reply, Mike!  I'm still tinkering and actually came up with a really simple solution. I'm going to test it but essentially what I did is as follows:
    Since I know the first date, and all subsequent dates are built off of the first date then I am using the following function in a!forEach - firstDate + sum(index(arrayOfNumberOfDays,enumerate(fv!index) + 1,{}))

    What this does is it takes our firstDate then adds the number of days for each iteration, combining previous number of days for all subsequent iterations.  Using my example above the second iteration will add 3 days, the third iteration will add 8 days.

    For now I think I've found a viable solution, but I will be keeping this post open a little while longer because I would love to see the inputs that others have.  Definitely an interesting problem!

Children
No Data