Posted in Dynamics 365, Machine Learning

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



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



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 = 



var url = "";

 var jsonObject 





































"GlobalParameters": {}


 var dataString = 


 if (service != null) 

{"POST", url, 






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





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



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






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

 try {


resultSentiment = 



resultProb = 






//alert(resultSentiment + " " + 








console.log('Unable to interpret 







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

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 =

    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 ( ”

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



  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.



Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s