Dynamics CRM meets Machine Learning–Part 3

So far in this series we have covered the business problem and machine learning setup that drives the score based on the email content. We have also discussed how this approach can be extended to calculate an aggregated happiness index based on various behaviour points from within Dynamics CRM. In this instalment, let us focus on deploying the ML (machine learning) module as web service, so that it can be consumed from within Dynamics CRM.

 

Deploying the ML module as a web service

Once you have tested your module and you are happy with results of the trained model, you can deploy it as a consumable web service by clicking on the Deploy Web Service button at the bottom of the ML Studio’s Experiment screen as shown below

deploy

 

After deployment, the web service is available in the Web Services section of the Azure ML Studio. You can click on the record and view its properties. In order to connect to this service you will need an API key which can be found in the properties as shown below

api key

 

The setup of this web service for our scenario is as below

Score – 0 for unhappy email

4 for a happy email

ws

 

Consuming the ML web service from within Dynamics CRM

 

Once the service has been deployed, it can be consumed from both Javascript and server side code (e.g. a plugin or a custom workflow activity). To keep it simple we will consume it from within the JavaScript

The following script on the email form can be used to call the service and get the score. Here we are sending the body in the tweet_text parameter and retrieving the results in JSON format.

 

sendRequest: function (text) {

 

var service = 

AzureScript.getRequestObject();

 

var url = "https://ussouthcentral.services.azureml.net/workspaces/xxxxxxxxxxxxxxxxxxx/services/xxxxxxxxxxxxxxx/execute?api-version=2.0&details=true";

 var jsonObject 

=

 

{

 

"Inputs": 

{

 

"input1": 

{

 

"ColumnNames": 

[

 

"sentiment_label",

 

"tweet_text"

 

],

 

"Values": 

[

 

[

 

null,

 

text

 

]

 

]

 

}

 

},

 

"GlobalParameters": {}

 };

 var dataString = 

JSON.stringify(jsonObject);

 if (service != null) 

{

 

service.open("POST", url, 

false);

 

service.setRequestHeader("X-Requested-Width", 

"XMLHttpRequest");

 

service.setRequestHeader("Authorization", "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

 

service.setRequestHeader("Accept", 

"application/json");

 

service.setRequestHeader("Content-Type", "application/json; 

charset=utf-8");

 

service.setRequestHeader("Content-Length", dataString.length);

 

service.send(dataString);

 //Recieve 

result

 var 

requestResults = eval('(' + service.responseText + ')');

 try {

 

resultSentiment = 

requestResults.Results.output1.value.Values[0][1];

 

resultProb = 

requestResults.Results.output1.value.Values[0][2];

 

return 

resultSentiment;

 

//alert(resultSentiment + " " + 

resultProb);

 

}

 catch 

(err) 

{

 

console.log('Unable to interpret 

result');

 

}

}

}

 

In the next part we will see how this score gets used within Dynamics CRM

Advertisements

6 thoughts on “Dynamics CRM meets Machine Learning–Part 3

  1. Hello Manny,

    I am getting an error while consuming web service by using java script at below line.
    var service =
    AzureScript.getRequestObject();

    below is the error :

    “One of the scripts for this record has caused an error. For more details, download the log file.
    ReferenceError: ‘AzureScript’ is undefined at ff (https://crmservicebus.crm8.dynamics.com/%7B636045155620000360%7D/WebResources/new_callwebservice?ver=1818182756:5:5) ”

    Can you let me know from where i can download this reference /or /how can i resolve this.

    Thanks,
    Rinkesh

    Like

  2. Are there examples on accessing the ML Web Service from a Dynamics 365 Custom Workflow? Certain DLLs are not allowed and I can’t use client.postasjsonasync in a D365 custom workflow. I’m able to build the Request Input but not sure how to call the web service with it.

    Thanks

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s