michaelremus

For-Each as an alternative to State Machine

Blog Post created by michaelremus on Apr 20, 2015

I needed to use state machine for my workflow, but for 1 reason or another, I cannot make it work. It's always stuck on the 'State Machine' part, where it doesn't even process a single branch/state. I read a few suggestions on how to solve these problem, but as it doesn't apply to my situation, so I looked for a workaround. Please do note, this is not an elegant solution, but it works, and it seems to work fast!

 

Solution concept: State machine is like an infinite loop, it keeps repeating/jumping states until certain exit conditions are met. Ergo, I can create an infinite loop using the various looping options (although For-Each is used as it seemed to by-pass the Safe Looping feature), until I want to loop to terminate. I'll use switch to go to different 'States' but other branching will work as well.

 

Example State Machine

Let's say we have 3 number of steps (States) that need to be met before proceeding further into the workflow, like illustrated below

 

statemachine_picture.png

 

In the above example, if conditions in State 1 are met, then it moves to State 2, and so on, until it reaches State 4 in which it exits the State machine and proceed with the rest of the workflow. In practice, it can jump in any order of the States as needed or keep staying at the same State.

 

Equivalent using For Each, Collection, and Switch

foreach01_pic.pngforeach02_pic.png

Solution break down:

Variables

  • LoopCol - collection type used for looping
  • LoopTxt - Single line for getting values from LoopCol
  • KillLoopYN - yes/no for killing or continuing loop
  • StateTxt - Single line for storing State

 

Steps:

  1. Initialize LoopCol by adding an arbitrary value (ex. Add 1)
  2. Set Initial state of StateTxt (State 1)
  3. Set KillLoopYN to No
  4. Configure ForEach loop with
    • Target Collection: LoopCol
    • Store result in: LoopTxt
    • Stop Processing: KillLoopYN
  5. Similar to the State Machine example, check for the desired condition. If you want to continue the loop, Set the desired State (StateTxt), Set KillLoopYN to No, Add an entry to the collection (LoopCol)
  6. To terminate the loop, either immediately set KillLoopYN to Yes, or change the State to the exit State (State 4 in this example)

 

I hope this helps those who are having trouble with State Machine. It's still better to use State Machine if it's working, but at least we have a work around if it's not. Thanks. My first post so please pardon me if not helpful. Thanks again.

Outcomes