4

I am building a Logic App that contains a foreach loop in a synchronous style (constrained to one execution at a time, concurrency=1). Within each foreach loop, there is a switch statement that calls different child Logic Apps depending on the contents of the item the foreach loop is iterating over.

I've discovered that if one of these child Logic Apps fail, the foreach loop continues on as if nothing happened, executing the subsequent tasks as determined by the contents of the array. I cannot find any way to halt (break) the execution of the foreach or terminate the whole logic app based on the failure of one of the child logic apps within the foreach loop.

If the same child Logic App were to fail without being inside the foreach loop, the execution of the parent Logic App would be halted. This seems a very strange design decision and creates an inconsistency with how Logic App execution behaves inside vs outside a loop.

Additional note: To add insult to injury, when you execute a child Logic App, even if it's an HTTP trigger and response, you don't appear to get access to the HTTP status code it returns in the calling (parent) Logic App. So even if I wanted to manually test every single case I have in my loop, I can't figure out how to do that.

M Rogers
  • 103
  • 1
  • 7
  • when you call a logic app from another logic app, it will return a 202. This is why your parent logic app is not stopping. Adding some http response action in your child logic app could solve the problem – Thomas Jan 16 '19 at 10:33
  • @Thomas It does not fix the problem. My child logic apps are already built with the appropriate HTTP responses for errors. – M Rogers Jan 18 '19 at 15:18

3 Answers3

3

I figured out a workaround.

I encapsulated the switch statement (within which my child logic apps are called) inside the foreach with a Scope action. The Scope action "inherits" a failure from anything inside it.

After the Scope, at the end of the foreach, I test the status of the Scope. If it's a failure, I set a "break" variable (initialized at the start of the Logic App to false) to "true".

At the beginning of each foreach iteration, I check the "break" variable. If it's "true", everything inside the foreach is skipped.

M Rogers
  • 103
  • 1
  • 7
0

I don't think there is a good way to do this at the moment. Normally I would use a terminate action that runs on failiure of previous action.. But terminate actions are not allowed within a foreach. Issue on the feedback forum

Maybe you can solve it by using a until action instead of foreach and have condition for when to break. And for the child logic app you can use a response action and set the response code to like 500 or something and check for that in the until.

Edit: Sorry missed you where not able to get the code in the parent.

You should be able to get status code from the child logic app if you invoke it via Http.. enter image description here

JohanSellberg
  • 2,423
  • 1
  • 21
  • 28
0

You can use the until connector instead.

Before using the connector, please create a variable that can use for setting boolean expressions (to convert any expression to boolean use bool() function from the Expressions section).
Then inside the until statement use that variable.

enter image description here

And the loop will be terminated once the condition variable will set to false.

Of course, you can also use the variable approach to check inside for each statement and don't do actions if it is false, but I think this will not help to stop for each loop from iterating over the array.

Arsen Khachaturyan
  • 7,904
  • 4
  • 42
  • 42