Workflow and ServiceDesk Community

 View Only
  • 1.  Is it possible to go back an iteration in a for each component?

    Posted Dec 10, 2012 04:24 PM

    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)

    Etc…

    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?



  • 2.  RE: Is it possible to go back an iteration in a for each component?
    Best Answer

    Posted Dec 11, 2012 06:20 AM

    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.

     



  • 3.  RE: Is it possible to go back an iteration in a for each component?

    Posted Dec 11, 2012 08:47 AM

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



  • 4.  RE: Is it possible to go back an iteration in a for each component?

    Posted Dec 11, 2012 09:03 AM

    Being picky here... but 0 to (count - 1) :)



  • 5.  RE: Is it possible to go back an iteration in a for each component?

    Posted Dec 11, 2012 09:53 AM

    Absolutely true ;-)



  • 6.  RE: Is it possible to go back an iteration in a for each component?

    Posted Dec 11, 2012 10:49 AM

    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



  • 7.  RE: Is it possible to go back an iteration in a for each component?

    Posted Dec 11, 2012 10:50 AM

    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.



  • 8.  RE: Is it possible to go back an iteration in a for each component?

    Posted Dec 11, 2012 10:55 AM

    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.



  • 9.  RE: Is it possible to go back an iteration in a for each component?

    Posted Dec 11, 2012 11:40 AM

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