Categories
Common Data Service Dynamics 365 Power Automate Power Platform

Power Automate your Demo Data

With the advent of Machine Learning and AI and all the insights that they provide, we have seen the addition of many new products to the Dynamics and Power Platform Eco-systems.

Dynamics 365 Customer Insights, Dynamics 365 Customer Service Insights and Dynamics 365 Sales Insights are some key examples of this.

A few months back I had a requirement to do some demonstrations using some of these Insight tools. The biggest problem with needing to demo Insights is that they require data on which to be insightful and unless you have oodles of existing data in your demo environment (or Production data that you’re prepared to share with others) the insights are either rather disappointing or don’t work at all, as they require a minimum number of records to analyse in many instances.

At the time I built a WPF app to set up and configure a whole lot of realistic demo data, thanks to Mockaroo and it’s great data generation capability. The problem with the WPF approach was that it required code and changes to code (and recompiling) if you wanted to alter or add to the data and the entities.

So… this week I decided to see if I could move this capability to Microsoft Power Automate (formerly Microsoft Flow) and use the Solution Framework to make it easily deploy-able to other environments.

WOW!!!

It was so super easy and effective. Below I will take you through the process (and share the result at the end)

The first step is to create a new Flow in Power Automate, two of the recently released features that I decided to use was the “Manually Trigger a Flow” trigger and the “Run Child Flow” action. It is important to note that you currently can’t add a flow that uses the “Manually Trigger a Flow” trigger to a Solution, however you can create one from the Solution, so all that means is a little bit of extra work in creating additional Flows as you can’t “Save As” and then add the other flow to the Solution (No Doubt this will be remedied in the future)

I used 2 Input Parameters, Mockaroo API Key (you’ll need to sign up for a free account at Mockaroo.com) and Number of Accounts to create.

Next you add an HTTP action and set the following :

  • Method = POST
  • URI = the Mockaroo API URI with the API Key and Record count added, so I used the Concat function as follows – concat(‘https://api.mockaroo.com/api/generate.json?key=’,triggerBody()[‘text’],’&count=’,triggerBody()[‘number’])
  • Body = a JSON object constructing the DataSet you need using the API Data Types provided by Mockaroo – https://mockaroo.com/api/docs#request_body
[
  {
    "type": "GUID",
    "name": "id"
  },
  {
    "type": "Fake Company Name",
    "name": "company_name"
  },
  {
    "type": "Street Address",
    "name": "Address"
  },
  {
    "type": "City",
    "name": "City"
  },
  {
    "type": "Postal Code",
    "name": "Postal_Code"
  },
  {
    "countries": [
      "New Zealand"
    ],
    "type": "Country",
    "name": "Country"
  },
  {
    "type": "Latitude",
    "name": "Latitude"
  },
  {
    "type": "Longitude",
    "name": "Longitude"
  },
  {
    "format": "+# (###) ###-####",
    "type": "Phone",
    "name": "Phone"
  },
  {
    "dataset": "Users",
    "column": "id",
    "selectionStyle": "random",
    "type": "Dataset Column",
    "name": "user_id"
  }
]

Next we Parse the Response JSON data so we can use it later in the Flow. You can generate the Schema from a Sample Response, the Schema used in this example is below :

{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "id": {
                "type": "string"
            },
            "company_name": {
                "type": "string"
            },
            "Address": {
                "type": "string"
            },
            "City": {
                "type": "string"
            },
            "Postal_Code": {
                "type": "string"
            },
            "Country": {
                "type": "string"
            },
            "Latitude": {
                "type": "number"
            },
            "Longitude": {
                "type": "number"
            },
            "Phone": {
                "type": "string"
            },
            "user_id": {
                "type": "string"
            }
        },
        "required": [
            "id",
            "company_name",
            "Address",
            "City",
            "Postal_Code",
            "Country",
            "Latitude",
            "Longitude",
            "Phone",
            "user_id"
        ]
    }
}

If you were paying attention to the Request Body earlier, you may have noticed that one of the columns was a user_id derived at random from a DataSet called Users. To allow referencing other records and creation of nice randomly linked data we can upload our generated data back to Mockaroo to reference in other requests. In order to do this we need to convert the JSON to a CSV and then send it back to Mockaroo, this is done in the next two steps.

Use the Convert to CSV Data Operation action and provide it the Body from the Parse JSON operation

Using another HTTP operation we can send the CSV data back to Mockaroo.

  • Method = POST
  • URI = concat(‘https://api.mockaroo.com/api/datasets/Accounts?key=’,triggerBody()[‘text’])
  • Header = content-type with a value of text/csv
  • Body = Output from Convert to CSV operation

We then use an Apply to Each with the Parsed JSON from earlier and a Common Data Service (current environment) connector to create the Account record in CDS.

We are then going to stitch together other similar Flows for Contacts, Products, Opportunities etc. using the “Run Child Flow” action, this requires us to end our Flow with a Response Step.

Stitching it all Together

To stitch it all together I created another Manually triggered flow which combines all the different input parameters.

We then check if the Input parameter is populated (or greater than zero) and call the child Flow.

The resulting Flow looks like this :

I hope this approach helps you with your Demo or Sample data scenarios.

My full Solution with all the Flows above is available for download here

Leave a Reply

Your email address will not be published. Required fields are marked *

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