Get Flow Run by Using Dynamics 365/Microsoft Dataverse Record ID as Parameter in Power Automate
There are several ways on how you can access the flow run history in Power Automate. In fact, to make things easier for me, I created a solution and wrote a blog post about how to Access Flow Run History within a Record in Dynamics 365/Microsoft Dataverse (check that out if that is of interest). However, the solution I did is not for everyone especially if there are tons of existing flows in the system already.
Another quick way to access the flow run (especially for those who need to find the flow run with the record ID for troubleshooting purposes) is by
exporting the flow run history
and find (Ctrl + F) the record GUID in the .csv file to identify the URL of a particular run. But this approach has a limitation as it only returns the last 100 records.
With Stefan Strube's
custom connector to get flow run history, I created an on-demand flow to find the flow runs by providing the
name of the flow and GUID of the record.
Here is the summary of the flow:
🛈 Disclaimer
The API endpoint to retrieve the flow runs is not officially documented in Microsoft Docs even though it is used by the Get-FlowRun cmdlet from Microsoft.PowerApps.PowerShell package. The ItemInternalId property of the trigger output is not available in the dynamic values and is not documented either. The undocumented API, URLs and the properties used in this solution may change without notice and it may stop working, so use at your own risk.
The API endpoint to retrieve the flow runs is not officially documented in Microsoft Docs even though it is used by the Get-FlowRun cmdlet from Microsoft.PowerApps.PowerShell package. The ItemInternalId property of the trigger output is not available in the dynamic values and is not documented either. The undocumented API, URLs and the properties used in this solution may change without notice and it may stop working, so use at your own risk.
1. Get the WorkflowIdUnique of the flow by the flow name
The sample solution flow is triggered manually but you can extend it and set it up as a recurrence flow and have it notify you of any failure in a shorter timeframe (e.g.
hourly) for mission-critical flows instead of relying on the weekly emails.
The first parameter is the name of the flow and the second one is the GUID
of the record. You can get the GUID of the record by using the "Record ID"
action from the
Level Up browser extension
by
Natraj Yegnaraman
or copy the GUID value after "id=" parameter in the URL in Unified
Interface.
Flow Run History array will hold the information of the run history
in JSON array format.
The next step is to use the List Rows action of Microsoft Dataverse connector to get the WorkflowIdUnique of the flow by the flow name
which will be used in the custom connector to get the flow runs.
2. Get the flow run history using the custom connector
This step will require the
custom connector
which contains the Get Flow Runs action. You can either create
the custom connector by following
this blog post
or import the solution from this link. Either way, you will need to
register an application with the Azure Active Directory service and grant
permission for the flow service to use in the custom connector. From the app registration, get the
Client Id and Client Secret to fill in the custom connector as well as the
Resource URL with static text "https://service.flow.microsoft.com/".
⚠ Important
Make sure to add the Redirect URI "https://global.consent.azure-apim.net/redirect" in the app registration > Authentication by following this documentation. Otherwise, authentication of the connector will fail with the following error.
Make sure to add the Redirect URI "https://global.consent.azure-apim.net/redirect" in the app registration > Authentication by following this documentation. Otherwise, authentication of the connector will fail with the following error.
The environment_name parameter is populated with the current environment
GUID value from the trigger.
workflow()['tags']['environmentName']
And the the flow_name is populated with the WorkflowIdUnique of the first record from the
List flow step.
first(outputs('List_flows_by_flow_name')?['body/value'])?['workflowidunique']
The Parse JSON action is used in the next step to
process the output of the custom connector and use in the upcoming steps.
3. Fetch output JSON of trigger for each flow run using HTTP GET method
One of the properties of the flow run JSON output from the custom
connector is the trigger URI from the outputsLink object. Calling that URL will respond with the JSON output of the trigger which contains the record ID.
items('Apply_to_each_flow_run')?['properties']?['trigger']?['outputsLink']?['uri']
4. Add flow run URL into array variable if the trigger is related to the record
The record GUID is stored in the ItemInternalId of the response JSON and check if that value is same as the Record ID input parameter.
If the record IDs match, add the URL to open the flow run into the array variable together with some other useful information such as start/end times, status, etc.
{
"RunID": "@{items('Apply_to_each_flow_run')?['name']}",
"RunURL": "@{replace(items('Apply_to_each_flow_run')?['id'], '/providers/Microsoft.ProcessSimple', 'https://flow.microsoft.com/manage')}",
"StartTime": @{items('Apply_to_each_flow_run')?['properties']?['startTime']},
"EndTime": @{items('Apply_to_each_flow_run')?['properties']?['endTime']},
"Status": @{items('Apply_to_each_flow_run')?['properties']?['status']}
}
🛈 Note
The StartTime and EndTime are in UTC and the values need to be converted into the local timezone.
The StartTime and EndTime are in UTC and the values need to be converted into the local timezone.
The end result of the array variable will look like this. The flow run URL can be opened by Ctrl + clicking on the URL in the Compose step for one-time troubleshooting purpose. You can also extend this solution further by using Create HTML Table action and email to yourself or post a message to a channel in Teams.
Summary
By using the custom connector based on the undocumented API to get flow run history and my above solution, we can now query the flow runs within the last 28 days (max run history retention) by providing the name of the flow and GUID of the record.
You can download the unmanaged solution from my GitHub repository via this link.
These connectors have changed
ReplyDeleteAdded the connector names and updated the screenshots.
Delete