This post is the final part of our two-part series on Implementing Machine Learning in D365. Head here to read part one if you haven’t been following along yet.
In part one, we built an Azure Machine Learning (AML) algorithm against our Opportunity data in D365 to come up with a probability that the Opportunity will win. In this post, we will use AML to generate a web service for us and then we will use Microsoft Flow to retrieve the probability and update newly created Opportunities with the value.
Within AML, after you successfully ran the experiment, select Set Up Web Service –> Predictive Web Service
Once it is finished, you will then have to click Run to go through the experiment again. Once that is finished you can click Deploy Web Service –> Deploy Web Service [New] Preview. You will have to create a new Price Plan (fortunately there is a free one with 1,000 transactions) and then click Deploy. Once it is deployed, you will want to click Use Web Service. On the next page you will want to make note of the Primary Key and Request-Response values as we will need this shortly.
Next, head over to Microsoft Flow and sign in with a Microsoft account. Click “Create from blank” and then search for the “When a record is created” for D365 trigger.
You will need to authenticate to D365, select your organization and then select the Opportunities entity.
Click + New step –> Add an action. Search for and select the “HTTP – HTTP” connector. We will use this to call our AML web service that we just deployed earlier. Set the Method to Post and the Uri to the value of the “Request-Response” that we noted earlier from AML. Next, add a header and set the key to ‘Authorization’ and set the value to ‘Bearer xxx’ where ‘xxx’ is the value of “Primary Key” that we noted earlier from AML. Then add a new header value with a key “Content-Type” and value “application/json”. Lastly, set the Body to the following:
{
“Inputs”: {
“input1: [
{
“description”: <description>,
“estimatedvalue”: <estimatedvalue>,
“statecode”: 0
}
]
},
“GlobalParameters”: {}
}
Using the dynamic content helper on the right, replace <description> from the Body by selecting Description from the list of dynamic content. Do the same for the <estimatedvalue> so that the whole HTTP action looks like the following screenshot:
Next, click + New Step –> Add an action and select Parse JSON. Using the dynamic content helper, set the Content field to the Body from the HTTP action. Set the Schema field to the following text:
{
"type": "object",
"properties": {
"Results": {
"type": "object",
"properties": {
"output1": {
"type": "array",
"items": {
"type": "object",
"properties": {
"estimatedvalue": {
"type": "string"
},
"description": {
"type": "string"
},
"statecode": {
"type": "string"
},
"Scored Labels": {
"type": "string"
},
"Scored Probabilities": {
"type": "string"
}
},
"required": [
"estimatedvalue",
"description",
"statecode",
"Scored Labels",
"Scored Probabilities"
]
}
}
}
}
}
}
This will help parse the response from the AML web service call so we can get to the actual probability value that is returned.
Lastly, click + New Step –> Add an action and select the “Update a record (V2)” for D365. Set your organization and the Opportunities entity again. Then using the dynamic content helper, set the required fields to the respective values from our first trigger from D365. For the probability, I created a custom Decimal field called Predicted Probability. This field will show up in the “Show advanced options” section in Flow. Click that and find the Predicted Probability field and set the expression to the following:
mul(float(body('Parse_JSON')?['Results']?['output1'][0]['Scored Probabilities']), 100)
This expression is using the Parse JSON action we added to get to the Score Probabilities value from AML, converting it to a float and then multiplying by 100 to show as a percentage in D365. The “Update a record (V2)” trigger should look like the following screenshots:
Now you can save the flow and make sure the trigger is set to “On”. Now in D365, we can create a new opportunity. I set the Description to “Wine” with an Est. Revenue of $90. Back in Flow, you can check the Run History and make sure the Flow ran and was successful. It may take a minute or two for it to run.
Once you see a successful process in Flow, refresh your D365 Opportunity and you should see the Predicted Probability updated.
Now we have a complete round-trip from getting the data out of D365 to train it and then pushing the predicted probability back into D365. For existing records, you will want to build a process to retrieve them all and call the AML web service for each one.
Azure Machine Learning has many different algorithms (here is a nice cheat sheet) for all different types of business processes. This is just one example and will hopefully give you an idea of what you can accomplish in your own organization. If you have an questions or are interested in enhancing your organization with machine learning, contact us.
Happy Machine Learning!