Today, I bumped into the result function in Flow. We can now collect failed action details directly within the flow and report the failures in flows.
Failures in Flows
Table of Contents
When things go wrong in your flow, there are some error message displayed in your Flow Run, however this means that a user needs to go to the flow run and find out where things went wrong.
Quite often a user interested in the problem might not even have access to the flow run. Wouldn’t it be nice if you could send the error message from failed actions to the user or administrator so that failures in flows become more visible?
My regular followers will be aware of the Try-Catch pattern in Power Automate that I cannot stop talking about. The try catch, will already send the URL to the flow run, but now we can enhance this with the details of the failing action.
Collecting the result of a failed action
In the flow below the Compose action is failing. And the Compose 2 action is collecting the error message generated by the first Compose action
Isn’t this what we have been waiting for for a long time?
The Result function
The result function is slightly harder to use than you might hope for. Although when you know how to use it it is easy to implement.
According to the result function documentation, it returns the result of a scoped action.
If you refer to an action that isn’t a scoped action then you will see an error like the one below when you attempt to save the flow:
So what is a scoped action? A Scoped action can be an Apply to each, Until or a Scope action.
This is going to open doors!
In my earlier example, all I had to do is set the Run after settings for my Compose 2 action to include to run on failure of the Scope and my result function can now give me the details of the action that failed.
Time to look at the output form the results function
[ { "name": "Compose", "startTime": "2020-03-06T09:42:59.1275689Z", "endTime": "2020-03-06T09:42:59.1275689Z", "trackingId": "06b9b7cc-f072-46d0-aa4f-b866ce7b2230", "clientTrackingId": "08586181191065299168785271735CU71", "clientKeywords": [ "testFlow" ], "code": "BadRequest", "status": "Failed", "error": { "code": "InvalidTemplate", "message": "Unable to process template language expressions in action 'Compose' inputs at line '1' and column '2195': 'The template language expression 'split('tyrewftysdgjfhgjdsgjhdyufy','a')[3]' cannot be evaluated because array index '3' is outside bounds (0, 0) of array. Please see https://aka.ms/logicexpressions for usage details.'." } } ]
Multiple actions in a Scope
Time to add a second action inside my scope.
I added a Send me an email notification action and in the flow run I can see that this action worked while my Compose still fails.
The Compose 2 action now returns the result of all the actions, including the successful ones.
[ { "name": "Send_me_an_email_notification", "inputs": { "method": "post", "body": { "notificationSubject": "Test Email", "notificationBody": "Test email body" }, "path": "/sendEmailNotification", "host": { "api": { "runtimeUrl": "https://unitedstates-002.azure-apim.net/apim/flowpush" }, "connection": { "name": "/providers/Microsoft.PowerApps/apis/shared_flowpush/connections/shared-flowpush-f471b5e3-04fd-43ff-b943-b4b566dd1dd1" } }, "authentication": { "value": "*sanitized*", "type": "Raw" } }, "outputs": { "statusCode": 200, "headers": { "Pragma": "no-cache", "x-ms-request-id": "westus:eb24befa-3cba-4ffa-9479-d00dc320e015", "x-ms-correlation-request-id": "eb24befa-3cba-4ffa-9479-d00dc320e015", "x-ms-flow-mobile-ios-version": "1.3.0", "x-ms-flow-routing-request-id": "WESTUS:20200306T094725Z:eb24befa-3cba-4ffa-9479-d00dc320e015", "Strict-Transport-Security": "max-age=31536000; includeSubDomains", "X-Content-Type-Options": "nosniff", "Timing-Allow-Origin": "*", "x-ms-apihub-cached-response": "false", "Cache-Control": "no-cache", "Date": "Fri, 06 Mar 2020 09:47:25 GMT", "Content-Length": "0", "Expires": "-1" } }, "startTime": "2020-03-06T09:47:23.9432031Z", "endTime": "2020-03-06T09:47:25.1776008Z", "trackingId": "1382cb46-803b-4be1-8697-dc448deaf3e7", "clientTrackingId": "08586181188417586183991966894CU91", "code": "OK", "status": "Succeeded" }, { "name": "Compose", "startTime": "2020-03-06T09:47:25.2088528Z", "endTime": "2020-03-06T09:47:25.2088528Z", "trackingId": "0880bf08-aafa-42e2-b43b-ed89e3448e7d", "clientTrackingId": "08586181188417586183991966894CU91", "code": "BadRequest", "status": "Failed", "error": { "code": "InvalidTemplate", "message": "Unable to process template language expressions in action 'Compose' inputs at line '1' and column '2194': 'The template language expression 'split('tyrewftysdgjfhgjdsgjhdyufy','a')[3]' cannot be evaluated because array index '3' is outside bounds (0, 0) of array. Please see https://aka.ms/logicexpressions for usage details.'." } } ]
Finding the failed actions
Some thoughts
The above idea of collecting the results of the actions should of course be included in the Try Catch pattern, however for the simplicity of this post I decided not to clutter this post.
Result function only returns 1 level of actions, but the actual error is in further actions. How to get detail of error in further actions.
This is indeed one of the limitations. Hence I would only use this where really needed as the error handling becomes a lot of overhead. For general error handling I would use the Try-Catch pattern.
How do you handle nested actions? I am finding that, depending how deep the nested actions go, it has a failure at each level, but really only the last failure provides the true failure or error message. The only way I was able to get any of that was to union all my nested actions, which returns all the failures like the one that matters and the ones that dont. This method is not dynamic so you have to manually add each nested action to the union. I would like to find a way to get the last error message when there are nested actions.
In general I would recommend to avoid nested actions anyway. Child flows are a better way. Potentially you could also use the stack trace solution that I blogged about a while back
https://sharepains.com/2023/03/31/create-a-stack-trace-in-power-automate-flows/