r/PowerAutomate • • 6d ago

Nesting Limit

I know the nesting limit of 8 has been raised before, but the solutions I've seen is to keep conditions in series, rather than nested, and use a boolean variable like Continue to decide whether subsequent conditions run.

I've tried this, but am using the FALSE branches to add to an error log. If the conditions are in series, one loop can enter multiple FALSE branches, and be added to the log multiple times.

Has anyone found a fix for this?

TIA!

3 Upvotes

13 comments sorted by

1

u/Reasonable-Beyond855 6d ago edited 6d ago

Best I can think of is adding IF statements as expressions in the error log values, rather than through the flow control - but this would mean subsequent steps are still processed after an error, which is inefficient.

1

u/Reasonable-Beyond855 6d ago edited 6d ago

For anyone with the same problem, another idea would be to create a compose statement which acts as the TRUE/FALSE eg/ checking if an array is populated: '1 / length(array)' would error if the array is empty. You can then use Run After complete for TRUE, and after fail for FALSE.

1

u/SrBlackVoid 6d ago

I have written probably hundreds of Flows at this point, and have built some pretty complex stuff. Some of them perform entire data syncs between systems.

I don't think I've ever gone more than 2-3 condition levels deep. When someone in our org mentioned this 'nesting limit', I didn't even know that was a thing.

I would say if you're going more than 3 levels deep in a Flow, you probably need to consider refactoring, because there is likely a better way to structure it.

1

u/Reasonable-Beyond855 6d ago

Mine's also a system integration :)

Interested to know how you're structuring this instead. How do you evaluate the different outcomes and add these to a log?

eg/ results since last run date (YES/NO) > user ID exists in source system (YES/NO) > user ID found in target system (YES/NO) > Data already synced for user (YES/NO) (to prevent duplicates)

1

u/SrBlackVoid 6d ago edited 6d ago

Offhand without seeing it (I'm open to seeing a mock-up of the current approach), it sounds like you could benefit more from a filter-first approach.

So instead of this (what it sounds like to me, and gonna use pseudo-PowerShell syntax for this bc I'm on a phone):

foreach ($entry in $runs) {
  if (condition1) {
    if (condition2) {
       Etc...
    }
  }
}

Try something like this:

$batch1 = Filter($runs, condition1)
# Perform whatever actions on entries that satisfy condition1

# If condition2 actions rely on condition1 results
$batch2 = Filter($batch1, condition2)
# Perform whatever actions rely on condition2 results
# Rinse and repeat

Filter-first should be the recommended approach anyway for condition handling on large sets, but in this instance you're replacing nesting conditions with stacked Filter actions, which has no limit (other than sheer action limits) because they're not nested. Hopefully that makes sense 🙂

1

u/Reasonable-Beyond855 5d ago

Thank you!

The problem is if filter 1 returns no results, and you try to run a FOR EACH on the results, it seems to just hang and never reach the end of the flow. I had a condition to check the arrays not empty to avoid this. Is there another way around it?

1

u/Reasonable-Beyond855 5d ago

ignore that - it was taking a long time for another reason. It worked to remove the conditions which check for empty arrays

1

u/SrBlackVoid 5d ago

... That don't sound right 🤔 If the inputs for an 'Apply to Each' is empty, it should just read as completed and move on. Only thing I can think of that might be causing that is checking the RunAfter settings on the action after the loop. Or if you're saying none of the other actions are running because they all inherit from the first filter results, that makes sense. The filters act as gates to apply the same steps to all that fit the condition, so if the first gate blocks everything and all the other steps only operate on what comes out of that gate, they're not going to do anything.

If you want, DM me a screenshot of the Flow or a general workup of it and I'll be happy to take a look at it.

1

u/Reasonable-Beyond855 5d ago

thank you - appreciate the help.. this wasn't the case, the flow just took 50 mins to run (it's a big dataset), so I thought it had stalled.

Removing the conditions (at least on the empty array checks) worked. Still need to keep some as gates to stop it executing remaining steps unnecessarily, but we're beneath the limit of 8 now at least.

Will DM you anyway, as interested in how to eliminate conditions further :)

1

u/ryguy694 6d ago

But have you ever heard of a switch?

1

u/Reasonable-Beyond855 6d ago edited 6d ago

Switch only evaluates different states of one input as far as I'm aware - i'm using different inputs for each condition.

Maybe you know a way of using it that I don't?

1

u/ryguy694 5d ago

Nest a switch or two inside each other usually goes way farther than binary conditions.

Option 1: compose a complex formula that outputs switch cases.

If a then 1 else if b then 2 else if c then 3

Option 2: flip your conditions upside down, sometimes the deepest decision means you have less branches if you literally rotate your logic 180 and start from deeper vs top level.

1

u/SuitEvening48 21h ago

Since the empty-array checks are sorted, I'd make each stage produce two groups: records that pass, and records that fail that specific check. Log the failures with the record ID and check name, then feed only the passing group into the next stage; that way a record only gets its first failure logged.