cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
wstock
Helper I
Helper I

Getting stuck on a nested "FOR EACH" loop flow

I have a flow (well, actually its a Logic App) that I need to do the following tasks: 

 

1.  Download a list of users from a REST API.

2.  Compare that list of users to a sharepoint List

3.  If the user DOESN'T exist, ADD them to the sharepoint list.

4. If the user DOES exist, UPDATE the SharePoint list item.

 

I have the following flow worked out so far, and everything is working, assuming I am adding these users to a blank sharepoint list.  If I try to run this a second time, after all of the users are in the list, the flow just hangs in execution, and I generally terminate it around 8 or 9 minutes.   Essentially, the "add user if the don't exist" part is working, but the "update user if they do exist" isn't working....just hangs the flow....

1.png

2.png3.png

1 ACCEPTED SOLUTION

Accepted Solutions
DeepakS
Resident Rockstar
Resident Rockstar

Hi @wstock 

 

You should use items('For_each')?['ID'] in Id field. Also, as you said, you have 1400 items, so this may take a little bit to complete, so be patient. (once you run for 1400 items make sure you go back to MyFlow page don't wait on flow page)

 

But I would suggest running your flow for only a few (4-5) items to confirm it works. (that I think should work)

 

——————————————————
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up. 

View solution in original post

11 REPLIES 11
DeepakS
Resident Rockstar
Resident Rockstar

Hi @wstock 

 

What are you passing in "Id" field when updating, also how many total items you are trying to update or loop through?

 

——————————————————
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

 

 

Ah, crap.  That's what happens when you destroy/rebuild these things a 100 times...

 

for ID, I'm passing in the "Current Item" for the 2nd FOR loop.

 

There are about 1400 records in the API response and the sharepoint list.  It takes about 3 minutes for the whole thing to run when it is just adding new records (ie: the list is empty) but after I run it once (list populated) the second run is still going strong after 8 or 9 minutes so I've been (assuming it's hung) and cancelling the run. 

 

4.png

DeepakS
Resident Rockstar
Resident Rockstar

Hi @wstock 

 

You should use items('For_each')?['ID'] in Id field. Also, as you said, you have 1400 items, so this may take a little bit to complete, so be patient. (once you run for 1400 items make sure you go back to MyFlow page don't wait on flow page)

 

But I would suggest running your flow for only a few (4-5) items to confirm it works. (that I think should work)

 

——————————————————
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up. 

Ok, I will try your suggestions...one question i have with that in mind though, is how can i limit it to just 4 or 5 items for testing?  The API spits back everything...

Is there a way to only complete the flow on like the first 5 items or so of the JSON object/array?  I suppose I could use a counter and increment it in the loop and put a condition check to limit it at the number of loop iterations ... 


Also, my last run I let go for over 2 hours...it never completed. 

 

 

 

DeepakS
Resident Rockstar
Resident Rockstar

Hi @wstock 

You can try following available settings to reduce the number loops without doing a lot of changes to your flow:

 

Settings.png

 

or you need to hard code JSON.

 

——————————————————
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up. 

 

Ok, I made the changes you suggested, and limited my runs to 5 items, and it is still hanging up inside the loop and never finishing...

DeepakS
Resident Rockstar
Resident Rockstar

Hi @wstock 

 

I would wait until Flow either error out or successful. I think you also might need think about optimizing your comparison as you have a Loop in side another loop so total number of loops will be exponential high. 

 

——————————————————
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

Ok, I thanks for your help so far to this point...we're almost there.  I just need to troubleshoot one last issue...These conditions on both for loops aren't working as expected...but it is probably something i did...gonna take another look at it. 

 

Hi @DeepakS 

Sorry to dig this up again, but I had a question for you...I agree that the nested Loop in a Loop comparison is crazy long to compare.  Do you have a suggestion for how to optimize this ?   I basically need to look at the list of items in SharePoint, and compare it to the list I pull from the API.  Update any existing / changed items, and add any new items.  The only way I can see to do this is the nested For loop...but there has to be a better way...

 

DeepakS
Resident Rockstar
Resident Rockstar

Hi @wstock 

 

As i dont have all the background of the requirement but i would try to use Filter Array action to filter the Get Items results based on the API data and perform my actions: 

Example:

2020-02-25_23-07-10.png

 

-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

 

Ok, thanks.  I will take a stab at filtering it down some. 

 

Cheers!

Helpful resources

Announcements
October Events

Mark Your Calendars

So many events happening this month - don't miss out!

 WHAT’S NEXT AT MICROSOFT IGNITE 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Register for a Free Workshop.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

Users online (3,173)