The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Lisa Holenstein
ServiceNow Employee
ServiceNow Employee

Workflow Automation CoE > Flows > Go back to hierarchical lookup

 

Goal

In this Workflow Automation Center of Excellence article, you’ll learn to use “Go back to” flow logic with a flow variable to iterate up a hierarchy until a defined condition is met.

 

Technology and Use Case

This article is an addition to an older article about hierarchical lookup with the do the following until flow logic. With the introduction of the new go back to flow logic, we have a new way to loop in flows until we reach a record that matches our defined condition. We will only look at the updated looping design pattern and the other lessons learned in the previous article still apply:

  • Use decision tables
  • Be sure to cover edge cases
  • Create subflows to re-use logic in various scenarios

Workflow Studio is our central low-code workflow automation technology, where we build flows, playbooks, and decision tables. We’ll use the go back to flow logic and a flow variable which we can set with the corresponding flow action.

 

For this use case, we ultimately want to create an approval for a catalog item, where the approver is the first user up the management chain [sys_user.manager] with a title containing “VP”.

This flow logic pattern can also be used to look up other parent-child record relationships, for example looking up:

  • Cost center [cmn_cost_center] parent records with a certain Manager or Location
  • Looking up location [cmn_location] parent records until you reach the region or state level
  • Looking up group [sys_user_group] parent records
  • Looking up department [cmn_department] parent records
  • Etc.

 

Instructions

First, we’ll look at the users in our example scenario. As you can see, we have multiple layers of users that each have a manager until we reach John Retak, whose title is “VP, Finance and Securities”.

LisaHolenstein_0-1713958588841.png

 

Build the subflow

These are the steps to follow to build this subflow.

  1. Create a reference flow variable on the User table
  2. Initiate the flow variable from the subflow inputs
  3. Use an if branch to check if the manager fits the title condition.
  4. If yes, assign the manager to the subflow output.
  5. If not, update the flow variable with the manager’s manager.
  6. Go back to the if condition to repeat.

LisaHolenstein_1-1713958598845.pngLisaHolenstein_2-1713958606063.png

 

Step 1: Create the flow variable

To create a flow variable, click on the three dots in the top right corner. Click on ‘Flow Variables’ to open the modal.

LisaHolenstein_3-1713958616860.png

 

Click on the plus in the top right of the modal to add a flow variable. Configure it to be a Reference/Record on the User table.

LisaHolenstein_4-1713958623763.png

 

Step 2: Initiate the flow variable

Add a new flow logic step “Set Flow Variables” in your flow. Click on the plus in the top right corner of the step to add a new row. Choose the flow variable you created from the drop-down on the left. With data pill dot-walking, set it to the current user’s manager. In our example, this is Subflow - Inputs -> Start User -> Manager.

LisaHolenstein_5-1713958660467.png

 

Step 3: Use If logic to check if the title matches

As the next step, add a new flow logic step “If” with a condition to check if the next manager’s title contains the title we’re looking for from the subflow inputs.

LisaHolenstein_6-1713958669691.png

 

Step 4: Assign subflow output

If the condition is met, we populate the output with the flow variable.

LisaHolenstein_7-1713958680618.png

 

Step 5: Else set the flow variable

If the condition is not met, we will overwrite the flow variable with the next manager, one level up.

LisaHolenstein_8-1713958683243.png

 

Step 6: Loop back to the if condition check

Lastly, we’re looping back to the if step to see if the next manager stored in the flow variable will satisfy the condition.

LisaHolenstein_9-1713958688057.png

 

Step 7: Test the subflow

Now, we'll test this subflow for one of our test users. We can see that the first (and second) iteration did not satisfy the condition, but the third loop did.

LisaHolenstein_10-1713958693638.png

 

LisaHolenstein_11-1713958698253.png

 

LisaHolenstein_12-1713958704153.png

 

Check for more edge cases

What if none of the managers in the upstream management chain has a VP title? One way to catch this and make sure we don’t loop endlessly is to add a condition to check if there even is a next manager. Here you can either just end the flow and not provide an output or assign a fallback (approval) user. Optionally, use a decision table to determine the fallback user as described in the previous article.

LisaHolenstein_13-1713958707330.png

 

Conclusion

Let’s review our flow once more. We have a start user and title to look for. If the title of the start user’s manager matches the condition, we’re good to go and the flow is done. Otherwise, we’ll update the flow variable with the next manager in the hierarchy, check if there even is one and if yes, loop back to check for the title again.

LisaHolenstein_14-1713958711506.png

 

 

Center of Excellence Navigation

  1. Workflow Automation - Center of Excellence
  2. Workflow Automation Migration Considerations
  3. What's new for Workflow Automation?
  4. Resource Hubs
    1. Flows
    2. Decision Tables
    3. Playbooks
  5. FAQ
  6. Checklist
  7. Training
  8. Workflow Academy
Comments
Lisa Goldman
Kilo Sage

Hello @Lisa Holenstein 

 

I've been trying to get the subflow to work, but I keep getting the error: 'Max iteration limit exceeded.'

I might have messed something up in the flow—could you help me troubleshoot it? Thanks

 

LisaGoldman_0-1755556705077.png

 

Lisa Holenstein
ServiceNow Employee
ServiceNow Employee

Hi @Lisa Goldman ,

 

Make sure to provide valid exit conditions for your loop, in this case there must be an option that your if-condition can be true. If you're walking up a hierarchy, that can be either hitting a specific value for the field that you're checking OR the manager field being empty. (Also see the section in the article on edge cases). If there is no manager in your management chain that will suffice the condition, it will loop until it hits the looping limit. Another option is to add a counter flow variable that you increment each loop and exit after 5 loops max with a "no manager found that satisifies the criteria" message.

Version history
Last update:
‎02-14-2025 05:49 AM
Updated by:
Contributors