MSAutomate

Monitoring Flows in Power Automate

When you have important production flows running, it’s important to be notified when a flow fails whether that is via Teams, Slack, Email, Text Message, etc.

Luckily, there is an easy solution that we can build within Power Automate to send us customized alerting. For this write-up, I’ll be going over how to keep track of flows utilizing Dataverse.

Prerequisites

For this write-up, we’ll need the following:

  • Power Platform Environment with a Database
  • Power Automate Premium Licensing

Creating a Solution

Before we get started, we will be building this out inside a solution. Solutions are portable and allow you to move flows, apps, components, etc. to different environments with ease. You can learn more about solutions here.

  1. Go to the Power Automate Dashboard
  2. In the left-hand column, select Solutions
  3. On the top toolbar, select New Solution
  4. Name the Solution “Flow Monitor”
  5. For the publisher, select “CDS Default Publisher” or the “Default publisher for org########”
  6. Next, select the Flow Monitor solution you just created. You should now be at this screen:

Creating a Dataverse Table

To keep track of running flows and easily check them, we’ll be storing information in a Dataversse table. Dataverse lets you easily store and manage data within a table. You can learn more about Dataverse here.

  1. Within the Flow Monitor Solution, Select New at the top toolbar and select Table.

  2. Next, select ‘New table’ at the top of the toolbar. This will bring out a sidebar.
    1. For the table’s display name, we’ll use “Flow Monitor”
    2. For the Primary Name Column Display Name, we’ll use “Flow Name”
  3. Click Create. The creation process should only take a minute.
  4. Next, we are going to add a few custom columns.
  5. At the top of the toolbar, select ‘Add column’. This will open the following sidebar:

    We are going to create the following columns: RunID, FlowID, Environment, and FlowStatus. Repeat the process below.

    1. Enter in the column name in Display Name
    2. Set Data Type to Text (This should be the default already)
    3. Set Required to Required (Except for FlowStatus, leave this as optional).
    4. Select Done
  6. After you’ve created your two columns, select Save Table in the bottom-right corner.

Now that we have our Dataverse table created, go ahead and go back to the solution to prepare for the next step. You can do this by clicking on “Flow Monitor”:

Creating a Flow – Add Flow to Flow Monitor

Our next step is to create a flow that we can use to add flows we’d like to monitor into our Dataverse table. In order for this to function as intended, flows that you want to monitor will also need to be built within a solution.

  1. From our Flow Monitor Solution, select New a on the top toolbar and select Cloud Flow. This will open up a new flow in a new tab.
  2. Let’s name the flow “Add Flow to Flow Monitor”
  3. For the trigger, we will use the “When a HTTP request is received”.
  4. Next, paste the following JSON Schema into the trigger schema input:
    {
        "type": "object",
        "properties": {
            "FlowID": {
                "type": "string"
            },
            "RunID": {
                "type": "string"
            },
            "FlowName": {
                "type": "string"
            },
            "Environment": {
                "type": "string"
            }
        }
    }
  5. Next, select show advanced options on the HTTP trigger and set the method to POST.
  6. Next, select New Step. Then, select Microsoft Dataverse. This will most likely already be listed. If not, you can search for it.
  7. Next, select the “Add a new row” option
  8. For the table name, you can either find or search for the Flow Monitors table that we created earlier.
  9. You will see that Flow Name, FlowID, Environment, and RunID are required fields. These are all the fields that we need to input information into. Using the Dynamic Content pop-out, put the respective inputs into their respective Dataverse input boxes. It should look like this:
  10. Next, select advanced options within the “Add a new row” action. You should see our optional “Flow Status” field. Hard-code in the word Running.
  11. Save the flow. Once the flow has been saved successfully, copy the HTTP Post URL from the trigger.

That wraps up our “Add Flow to Flow Monitor” flow. Easy so far, right?

Create a Custom Connector – Flow API

In order to check a flow run, we need to access Microsoft’s Flow API. We can accomplish this with a Custom Connector. There are two parts to the custom connector, creating an Azure AD App Registration and the Custom connector itself.

Azure AD Application Registration for Flow API

  1. Open up Azure Active Directory Admin Center
  2. Under the Manage, select the App Registrations blade
  3. Select New Registration
    1. For the Name, I’ll be using “Flow API”
    2. For the redirect URI, use this URL: https://global.consent.azure-apim.net/redirect
  4. Register the app.
  5. Copy the Application (Client) ID. We will need these later on.
  6. Select the API Permissions blade then select Add a permission
  7. Select Flow Service, then select Delegated permissions.
  8. Check Flows.Read.All and Activity.Read.All. Then select Add Permissions.
  9. Next, select Grant admin consent for #Tenant Name# and select Yes once prompted. This will allow your API to access all Flow runs within your tenant.
  10. Next, go to the Certificates & secrets blade. Select New client secret. Choose an expiration of your choice.
  11. Copy the secret Value and Secret ID and store them in a secure password manager. You will not be able to see these values after leaving this screen.

Create a Custom Connector

Now that we have our Azure AD App registration configured, let’s go ahead and create the custom connector. To simplify this process, you can download the connectors JSON file here: Download Connector

  1. Go to the Power Automate Dashboard
  2. Expand Data and select Custom Connectors
  3. Select New custom connector in the top-right corner and select Import from an OpenAPI file
    1. Connector Name: Flow API
    2. Select the file that you downloaded
  4. Select Continue
  5. Select Security
  6. Under the OAuth2.0 Fields, paste in the following information that you copied down earlier:
    1. Client Id (Also known as Application Id)
    2. Client Secret
    3. Resource URL: https://service.flow.microsoft.com/
  7. Select Create Connector

Creating a Flow – Monitor Flow Monitor Table

The next flow that we will create will monitor the Dataverse table that we created earlier, Flow Monitor. This flow will run on a reoccurring schedule that will check on flows where their Flow Status is “Running”.

  1. In our Flow Monitor Solution, select New at the top of the toolbar then select Cloud Flow.
  2. Name the flow “Monitor Flow Monitor Table”
  3. For our trigger, search for “Recurrence” and select the only available option. For the interval and frequency, you can choose a time that best fits you. For this write-up, I’ll use 30 minutes.
  4. Next, select New step, then select Microsoft Dataverse. For the action, select List rows.
  5. Search for or find the flow monitor table in the drop-down. Once selected, expand advanced options if it doesn’t automatically expand.
  6. In the Filter rows input box, type in the following OData query: crfb2_flowstatus eq ‘Running’
    Note: You can obtain a column unique name from the Dataverse table.
  7. Next, select New Step and select the Custom tab. Select the Flow API custom connector we built earlier on.
  8. Select the Get Flow Run by Run ID action. If prompted, sign in with an account that has administrative privileges in Power Automate.
  9. Next, with the Dynamic Content pop-out, enter the Environment, FlowID, and RunID into their respective fields. This will place the action into an Apply to each automatically.
  10. Next, click add an action right below our Get Flow Run by RunID. Choose Condition.
  11. For the Condition, we will compare the Dataverse table Flow Status to the properties/status of the Get Flow Run by RunID. If you search for status, it should be the first one listed. You can also hover over it to make sure it ends in properties/status.
  12. Next, change the operator of the condition to “is not equal to”.
  13. Under the If Yes box, select Add an action.
  14. Choose Microsoft Dataverse then select Update a row. For the table, select Flow Monitors. For the row ID, search for Flow Monitor in the Dynamic content and select the Flow Monitor variable that says “Unique identifier for entity instances”.
  15. Next, select show advanced options and place the same status field that ends in property/status into the Flow Status field within our update Dataverse action. Again, this should be the first status listed in the dynamic content box.
  16. Save the flow.

Creating a Flow – Failure Notifications

Now that we have the monitoring flow built out, we can build a flow to send alerts. For this demonstration, I’ll be using Teams. You could also use Slack, Outlook, SMS, etc.

    1. From our Flow Monitor Solution, select New a on the top toolbar and select Cloud Flow. This will open up a new flow in a new tab.
    2. Let’s name the flow “Send Alert Notifications”
    3. For the trigger, we will use the Microsoft Dataverse “When a row is added, modified or deleted”.
    4. For the triggers change type, select modified.
    5. For the triggers table name, select Flow Monitors.
    6. For the scope, choose organization.
    7. Show the advanced options and set the Row Filter to the following OData Query: crfb2_flowstatus eq ‘Failed’
      Note: Your unique column name will be different than mine. You’ll need to retrieve it from the Table.
    8. Next, add a new step and search for Teams. Choose the “Post your own adaptive card as the Flow bot to a user”
      Note: You can either use your account or a service account to send this message.
    9. Use yourself as a recipient
    10. Paste the following JSON into the message field:
      {
        "type": "AdaptiveCard",
        "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
        "version": "1.2",
        "body": [
          {
            "type": "TextBlock",
            "text": "Flow Failure Notification",
            "wrap": true,
            "weight": "Bolder",
            "size": "Large"
          },
          {
            "type": "TextBlock",
            "text": "The following flow run has failed:",
            "wrap": true,
            "color": "Accent",
            "weight": "Bolder"
          },
          {
            "type": "FactSet",
            "facts": [
              {
                "title": "Flow Name:",
                "value": "VARIABLE HERE"
              },
              {
                "title": "Run ID:",
                "value": "VARIABLE HERE"
              }
            ]
          },
          {
            "type": "ActionSet",
            "actions": [
              {
                "type": "Action.OpenUrl",
                "title": "View Flow Run",
                "url": "VARIABLE HERE"
              }
            ]
          }
        ]
      }
    11. Replace the variables with dynamic content from our Dataverse trigger as shown below:

      For the action.HTTP URL: https://us.flow.microsoft.com/manage/environments/ENVIRONMENT/flows/FLOWID/runs/RUNID
    12. Save the flow

Now whenever a flow status changes to “Failed” within our Dataverse table, we will be sent an alert in Teams. We can then click the button within the adaptive card and be taken straight to the flow run:

Utilizing the Flow Monitor

Now that we have everything built out, let’s go over how we can add flows to our Dataverse table and begin monitoring them.

All we need is to add an HTTP action to the top of a flow that we want to monitor. Example:

For the HTTP Post, you’ll need the following:

  • Method: POST
  • URI: The HTTP Post URI from our Add Flow to Flow Monitor Trigger
  • Headers: Content-Type application/json
  • Body:
    {
      "FlowID": @{workflow().name},
      "RunID": @{workflow().run.name},
      "FlowName": @{workflow().tags.flowDisplayName},
      "Environment": @{workflow().tags.environmentName}
    }

That’s it! Quick and simple. Since this data is housed in Dataverse, you could also create yourself a PowerBI dashboard for reporting and metrics.

Jordan Bardwell

Add comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.

Most popular

Most discussed