It is absolutely amazing how many things Microsoft Flow can do. One of the most useful actions I have come across is making HTTP requests that integrate with Dynamics 365. Using Flow to make HTTP requests to external services allows for you to create loosely coupled integrations very easily. Once the solution feature is released in October 2018, it will also be easy to maintain. This post will show you how to make a simple HTTP request to one of the Cognitive Service API’s in response to a trigger within Dynamics 365.
The first thing to do is create the trigger for your Flow. A trigger in Flow is the initiator of the proceeding business logic in the Flow definition. The example in this post and that you will see below, is when a new ‘Email Message’ entity is created in Dynamics 365 CE. The configuration is pointing Flow to the right instance of Dynamics 365 CE and specifying the entity name. Configuration is different based on each trigger action.
Now is a good time to rename your blocks as you go, before you begin using them in your Flow. This is so they are more descriptive and memorable so that when reviewing the definition at a later date, it’s easy to pick up what the business logic is.
I’ve used this technique for connecting to the Cognitive Services in Azure and also a hosted Machine Learning Model in Azure. Select an action block and select HTTP. You will need to specify a few things here.
-> Enter the Method. This is normally POST if you are using the Cognitive Services or Machine Learning Web Service, for example
-> Enter the URI. This is the URL that your request is going to be sent to. If you are using Azure, this can normally be found in the documentation or getting started areas.
-> Enter the Authentication Keys on the Headers tab. The documentation for the API normally details the name of the key and where the key can be found. This key is usually linked to your subscription of the API your using.
-> Enter your request Body. This is normally in JSON format but depends on what API you are using.
It is always best to use a service like Postman to be able to check your credentials and URI is correct, so you don’t spend a huge amount of time thinking the Flow setup has been done incorrectly when you could be sending the wrong details in the header or body of the request. I also suggest using some test data when first starting out with the body so you know things are working fine.
A precursor to adding your final Action, you can add an action which will Parse your JSON response from the previous HTTP request. You need to search for ‘Data Operations’ or you can just search for ‘Parse’ and the ‘Data Operations’ action will appear. Add the ‘Content’ you wish to Parse and then enter the Schema. By far the best way to get the schema is to use a sample payload, which you should be able to get from any half documented API. It will then automatically create a schema for you to be able to Parse your response to. Parsing is particularly important to be able to obtain the specific values, based on the schema, to use in your final step and update fields with the specific data related to that field. You don’t normally want the entire body of a response dumped into a field (unless it’s for error checking) as your going to have to obtain the values from that response somewhere down the line.
Once you have completed this step, you can then add a final action which, in this scenario, is to create a new record in Dynamics 365 CE of the ‘Sentiment’ entity. Select ‘Create new record’ and find the Dynamics 365 – Create new record action. Enter the organisation name and entity name. Select ‘Show Advanced Options’ to open the full entity definition. Now you can add in the data from your previous action of Parsing the JSON to the values you need. This can be seen in the image below where we have ‘errors’, ‘id’ and ‘score’ which can all be specific values I can add into my record in Dynamics 365.
Ensure you have entered values for any mandatory fields on your record and save your Flow. It’s always a good idea to rename your Flow to something more memorable and descriptive.
When you save your record, depending on the API and Schema your using, normally API’s return a collection. A collection can contain more than one item. The Flow will pick this up and add an ‘Apply to Each’ loop automatically on your Flow definition, and even add in the iterator form your Parse JSON request. You can add this manually if you wish.
Go ahead and test your flow (which is really important) using the ‘Test’ function. Complete the trigger action and the Flow business logic will automatically run. You can see a completed and successful Flow output below where it has created a new entity (Sentiment) and allocated the score from my ‘Parse’ action.
That’s all there is to creating loosely coupled applications using Microsoft Flow. If you have any questions, please do leave them in the comments. Thank you for reading!