Video Screencast Help

Is it possible to go back an iteration in a for each component?

Created: 10 Dec 2012 • Updated: 11 Dec 2012 | 8 comments
This issue has been solved. See solution.

I’m currently using the “For each element in collection” component in my workflow. I want to have the option of going back one iteration. I know that it is logically possible; I just don’t know how to do it in workflow. Here's a brief example of how I want it to work.

Create “N” (variable equal to 0)

Loop starts

Form builder that displays the first element in the array (N=0)

User clicks next (path 1)

Add 1 to N

Loop continues

Form builder displays the second element in the array (N=1)

User clicks BACK (path 2)

Subtract 1 from N

Loops again

Form builder displays the first element in the array (N=0)


The problem I’m having with this is that I cannot get the “for each component” to use“N” for it's iterations. I need it to display the element of the array who’s numeric position equals “N”. Is there a way that I can do something like that? If not, is there another solution to my problem?

Comments 8 CommentsJump to latest comment

reecardo's picture

I'm thinking very high level here and haven't seen if it works... but I'd do something like the following:

On clicking Back:

1) Use a Index Of Item In Collection to see where you are

2) Use a Get Item By Index to navigate to (index - 1)

If you ever need to recover where you were when going back through the loop, you'd have to also do the following with the steps above:

3) Store each index that you've navigated to s.t. the For Each Item in Collection ignores it in the next step.

4) Come back through your collection via For Each Item In Collection, but skip any item you've preciously navigated to (via some Compare Rule against stored the index collection above)

This, of course, assumes that each time you go through the For Each Item In Collection, you march through the collection in the same order each time.

Hopefully this helps.

AnaMan's picture

Don't use a For Each Element In Collection at all.

Make a loop declaring your own iterator starting from 0 till collection Count property value and using a Compare Number component as a condition tester.

The current item in collection can be accessed using explicite iterator value (Get Item By Index) and relative items by using additional fixed offset.

oshgosh's picture

Thanks for the suggestions! I'll try both of these and get back to you guys.

oshgosh's picture

AnaMan: Your solution worked like a charm. Thanks a ton.

reecardo: I was in the process of trying yours, I got to steps 3 and 5, then my brain exploded. I wasn't sure what you ment by "s.t.". I really appreciate the feedback, but AnaMans solution seemed easier.

Thanks again guys smiley

reecardo's picture

s.t = such that

Anaman's solution is a bit more elegant... guess I was in the process of thinking about it too much while still trying to preserve the For Each Item In Collection. The main thing I was trying to do was allow the user to "recover" where he was in the loop if he clicked Back.

oshgosh's picture

Ah. I gotcha. I hid the "next" button if N = arraycount and the "back" button if N = 0.