Integration of Azure machine Learning with Dynamics 365

Hello guys,

Today we will talk about the hot topic of the moment: the Artificial Intelligence!
It's not a new topic but it's on the showcase since few weeks.

Now I will focus on some possibilities we have, to implement some custom AI scenarios with our Dynamics 365 platerform (CE in the article but I guess it's pretty similar with other modules).

I did a talk during the MS Experience 2018 in Paris about this topic, and I will share with you what I did / discovered about that topic (which was completely new to me !).

The demo scenario : Win Rate on opportunity

Well I see you coming, "that's a simple and well known scenario". I agree but the point is not to describe a scenario but more how to make Dynamics 365 and the Azure Machine Learning talk to each other.

The process for the demo was pretty simple :

  1. Update your Opportunity record
  2. Flow triggers and gather the needed data and sends it to the Azure Machine Learning with a HTTP Post request.
  3. The Machine Learning gets the data and treats it to give our Win Rate score in return
  4. Flow gets the result from the Machine Learning
  5. The oppportunity record gets updated with the Win Rate score retrieved.

In real action it gives something like :
The win rate changes from 50% to 62.50% after the data "Identify Sales Team" turned to completed since it's one data we are interested in on Machine Learning side.

I won't go in detail about the process with Flow since it has already been detailed in others articles :
Implementing Machine Learning in D365 part 1 and part 2 by Sonoma partners

Other integration possibilities ?

We will have a look to 3 others way (next to flow) which could let you communicate with the Azure Machine Learning and one possibility to improve the data process.

  1. Connect directly with Plugins
  2. Connect with Azure Functions
  3. Connect with Azure Service Bus
  4. Synchronize your data live

I will give you my analysis about each points.

1. Connect directly with Plugins

Advantages :

  • Keeping the plugin context while sending data to the Azure Machine Learning
  • Avoiding extra times before the process is triggered since it's a direct trigger
  • Fully customize the data you can send
  • Code necessary*

Attention points :

  • Make sure it's not blocking your synchronous plugin or run it async in order to not stuck the process.

2. Connect with Azure Functions

Advantages :

  • Out of the box integration with the Plugin Registration Tool since version 9.X and above
  • Keeping the plugin context while sending data to the Azure Machine Learning
  • Fully customize the data you can send
  • Code necessary*

3. Connect with Azure Service Bus

Advantages :

  • Out of the box integration with the Plugin Registration Tool since version 8.2 and above
  • Use the same Azure Service Bus message for several usage (multiple apps, multiple systems, ...)
  • Keeping the plugin context while sending data to the Azure Machine Learning
  • Fully customize the data you can send
  • Extra application necessary to integrate the Machine Learning result into the CRM.
  • Code necessary*

4. Synchronize your data live

You can synchronize your data from the CRM Instance to an Azure SQL Database.
If you want to achieve that the easiest way, you can check in the direction of Data Export Service

In few words, you can select the entities (change tracking needs to be enabled first) to be synchronized (takes milliseconds for a record). It also takes into account the added columns after the first sync is triggered.

I'm speaking of this tool now because it could be really useful to reload the Machine Learning dataset continuously so the prediction would be more and more accurate with the reality of your business !

Code necessary part :

In the previous part of the article, I mentioned the fact that some code are necessary in opposite to Flow to perform the connection between Dynamics 365 and the Machine Learning.

Here is a simple of code in C# :

static async Task CallingMachineLearning()  
{
    using (var client = new HttpClient())
    {
        var scoreRequest = new
        {
            Inputs = new Dictionary<string, List<Dictionary<string, string>>> () {
                {
                    "input1",
                    new List<Dictionary<string, string>>(){
                        new Dictionary<string, string>(){
                            { "estimatedvalue", "1234" },
                            { "statecode", "0" },
                            { "any column", "any data"  }
                        }
                    }
                },
            },
            GlobalParameters = new Dictionary<string, string>() {
            }
        };

        const string apiKey = "YOUR BEARER TOKEN HERE"; // Replace this with the API key for the web service
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Bearer", apiKey);
        client.BaseAddress = new Uri("https://REGION.services.azureml.net/workspaces/WORKSPACEID/services/SERVICEID/execute?api-version=2.0&format=swagger");

        // WARNING: The 'await' statement below can result in a deadlock
        // if you are calling this code from the UI thread of an ASP.Net application.
        // One way to address this would be to call ConfigureAwait(false)
        // so that the execution does not attempt to resume on the original context.
        // For instance, replace code such as:
        //      result = await DoSomeTask()
        // with the following:
        //      result = await DoSomeTask().ConfigureAwait(false)

        HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest);

        if (response.IsSuccessStatusCode)
        {
            string result = await response.Content.ReadAsStringAsync();
            Console.WriteLine("Result: {0}", result);
        }
        else
        {
            Console.WriteLine(string.Format("The request failed with status code: {0}", response.StatusCode));
            Console.WriteLine(response.Headers.ToString());

            string responseContent = await response.Content.ReadAsStringAsync();
            Console.WriteLine(responseContent);
        }
    }
}

Next steps :

From this point, if you chose the best option to integrate Dynamics 365 and Azure Machine Learning, you could imagine any possible scenario which could add extra value to your business.

Few examples :

  • Marketing personalization : Based on previous event feedbacks from client, you could use the Machine Learning to improve the personal exchanges and services or products to suggest to your client. He would be treated as an unique client compare to the other.
  • Sales cross sell & upsell : in order to keep good relationship with your client, you could build something with the Azure Machine Learning by sending all deals done with that clients in the past so you could keep suggesting new job opportunities and products to sell. A great way to keep priviledged relationship with the client.
  • Service analytics : We all know that the support received by a customer after a purchase is critically important. The AI could help the support teams to gather all common issues from the past and quickly suggest solution, this would save time for the support team and the client will feel lucky to get a quick answer too.

The only limit is your imagination and the time you can allocate to this new project ! :).

These are my two cents on that topic.

Hope this can help,
Happy CRM'in

Clement

Dynamics 365 CRM & Power Platform addict.