Suggestion on how to build a loop back in Flow Designer.

Daniel Borkowi1
Mega Sage

This article aims to provide guidance for working with the flow designer. I have often been asked this question in projects and I have also seen it a few times here in the community. So how do you jump back in the flow designer when certain actions need to go through again?

 

Common Requirement

 

You maybe remember the old times of Workflow editor where you easily could build in loop backs in your flows. An example requirement could be following:

  1. Someone (user A e.g. Technical Consultant) gets a task to create a design document which needs to be validated by someone else (user B e.g. Architect)
  2. So, after closing the task a Design check task will be created and assigned to the user B.
  3. If B completes the check without any concerns the workflow continues with the next steps
  4. If B does not accept the design the design task should be opened again and steps 1. and 2. should be repeated.

The complete flow is described in this activity diagram:

 


Flow_Repeat_Until.png

I know this is a bit contrived and could be also achieved with an approval, but it was only meant to illustrate similar problems.

 

This "rollback" was quite easy to build with the old workflow editor. But it’s easy in the Flow designer too. You just need to think a little bit like a programmer. I admit that sounds crazy, but it's not as hard as you might assume.

Seriously, I have been asked these questions several times by colleagues and read them here on the forum. I think for many developers the following is crystal clear, but maybe it helps someone anyway if I briefly outline the solution for such requirements here.

 

 

How to repeat and rollback steps within a flow?

 

As you know there is Flow Logic available to build a loop, like ‘For Each’ or ‘Do the following until’. For our example the second one is the useful function. The clou is, that the condition will be evaluated after all steps within the loop are executed. Means, although the condition is true before the loop will be entered, the loop will not be skipped directly. It will execute the steps within the loop at least one time. These steps can include, that the condition isn’t true afterwards and so the loop will be executed again. But be cautious don’t build infinity loops!

 

The important prerequisite is, that you can only use in the loop condition records which are created before the loop. That’s why we create the Architecture Task in the very beginning.

 

Setting the scene: The consultant creates a story, and the system automatically creates two task, one for the design and one for the architect to review the task. The story can go to state ready when the design is valid.

To summarize the steps of the described flow from the beginning. Until the architect sets the state of his task to complete the technical consultant needs to adjust the design – the consultant needs to work and rework on the design until the architect is happy.

 

Let’s build the flow for our example.

  1. Set the trigger: Story is created 
    DanielBorkowi1_1-1685093939970.png
  2. Add Action to create the Design task:
    DanielBorkowi1_2-1685093939971.png
  3. Add Action to create the Architecture task. We need this task already now, otherwise we couldn’t use it in loop in next step as condition:
    DanielBorkowi1_3-1685093939972.png
  4. Now the import part – the “Do the following Until” loop. We do again and again the following steps, until the architect closes his task with state complete. Even if the architect task would be completed already beforehand, this loop will be executing at least one time! The condition is evaluated at the end of the loop not before entering – programmer will know this as do-while loop.
    DanielBorkowi1_4-1685093939974.png
  5. Now we wait within the flow until the consultant completes his task.
    DanielBorkowi1_5-1685093939975.png
  6. The architect can now make his merciless judgment and gets his task set to status ready for it
    DanielBorkowi1_6-1685093939977.png
  7. We wait until the architect came to a result, either he accepts the design and sets the status to complete, or he rejects it.
    DanielBorkowi1_7-1685093939978.png
  8. If the Architect is not happy and cancels his task the flow sets the state of the Design task and Architecture task back to Draft.
    1. IF condition:
      DanielBorkowi1_8-1685093939979.png
    2. Update the design task in the THEN part of the if condition:
      DanielBorkowi1_9-1685093939981.png
    3. And another Update Task action within the IF branch: 


      DanielBorkowi1_0-1685094825337.png

       

  9. With that we are at the end of the loop and the defined condition will be evaluated. Now two way the flow can follow up:
    1. If step 8.a – 8.c happened the condition of the loop is not true and all the steps beginning with step 5 will be repeated – rework until architect is happy.
    2. If the architect closed his task with complete in step 7 – he is happy, and the loop will be exited.
  10. After the loop the flow finishes with updating the story to state ready and the implementation of the awesome design can start. 
    DanielBorkowi1_1-1685094858955.png

     

 

Here is the whole flow in one picture:

 


DanielBorkowi1_15-1685094232013.png

 

The update set for this flow is attached.

 

Hope this will help someone. If yes please click the Thumbs up.

 

Greets

Daniel

1 ACCEPTED SOLUTION

Daniel Borkowi1
Mega Sage

See Article in origin post.

View solution in original post

8 REPLIES 8

josdsi
Tera Contributor

Vielen Dank, Daniel! So findet man sich dann wohl in der Community wieder 🙂

Gerne, wenn ich nur wüsste, wer sich hinter @josdsi verbirgt 🙂

mohamed shibl
Tera Contributor

Hi Daniel, 

Thank you for this great article! 

My concern is, What about the frequency of looping and also the limit of maximum loops which is 1000 by default in ServiceNow for "do the following until" as per my knowledge? 

 

Hi @mohamed shibl , in this example (or similar use cases) I hope that the task will be solved in 1000 iterations. But yes this is the limit you should have in mind. By the way the limit is defined in property sn_flow_designer.max_iterations. You can have a iteration count and check it against the max_iterations property - if limit is almost reached you may do some exception handling or escalations.

 

Greets 

Daniel