Salesforce ~ Microsoft Dynamic CRM Integration Part 1

part1_1

“Microsoft Dynamic 365” is ERP and CRM solutions provided by Microsoft. It provides solutions for different apps like Sales, Customer Service, Field Service, Project Service Automation, Finance & Operations, Talent and Retail.

Microsoft “Common Data Service Web API” is a REST API. This Web API provides a RESTful programming experience, we can perform the different operations on organization data with this API.

SignUp 👉 MS Dynamic trail account for development purpose here:

1

After sign-up login with the credentials | You will see the page shown in the screen below | Select the app.

2

3

Now, GoTo Azure portal and follow the steps given below to create an App for integration purpose.

1. Click on Azure Active Directory | App registrations | New registration.

4

2. Enter the App Name and click on Register.

5

3. Copy the client ID (This will be used later in the integration code)

6

4. Click on Certification & secretes | Follow the steps shown in the screenshot. | Copy the client secrete (later we will use this in integration code).

8

5. Click on API permission and add API Permissions.

9

6. Click on Manifest and update the settings shown in the screenshot below. | Click on Save.

7

Yeah! done with all the settings at the Microsoft side. 🙂

The first step for integrating any third party API is Authorization, so let’s authenticate the app (MS Dynamic CRM) and get the AccessToken by making HTTP request/callout using APEX code in Salesforce.

String url = 'https://login.microsoftonline.com/common/oauth2/token';
String clientid = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // paste client ID
String clientSec = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'; //paste client Secrete
String username = 'XXXXXX'; //put your username
String password = 'XXXXXX'; //put your password
String resource = 'https://yourdomain.crmX.dynamics.com/';
String body = 'client_id='+clientid+'&client_secret='+clientSec+'&username='+username+'&password='+password+'&resource='+resource+'&grant_type=password';
HttpRequest req=new HttpRequest();
req.setEndpoint(url);
req.setHeader('Accept','application/json');
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
req.setBody(body);
req.setMethod('POST');
Http h=new Http();
HttpResponse res=h.send(req);
system.debug('Response Body: = > '+res.getBody());
// Parse JSON response to get AccessToken.
JSONParser parser = JSON.createParser(res.getBody());
String accessToken = '';
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && 
(parser.getText() == 'access_token')) {
// Get the value.
parser.nextToken();
accessToken += parser.getText();
}
}
system.debug('Access Token =: ' + accessToken);

Execute the above code in Salesforce developer console and get the AccessToken.

Response body:

{
  "token_type": "Bearer",
  "scope": "user_impersonation",
  "expires_in": "3600",
  "ext_expires_in": "3600",
  "expires_on": "1562850806",
  "not_before": "1562846906",
  "resource": "https://XXXXXX.crmX.dynamics.com/",
  "access_token": "eyJ0eXAiOiJKV1QiLCfdgdfgJhbGciOiJSUzI1NiIsIng1dCI6InU0T2ZORlBId0VCb3NIanRyYXVPYlY4NExuWSIsImtpZCI6InU0T2ZORlBId0VC...."
  "refresh_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCIeyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI...."
}

Now, we got the AccessToken, we can make another request to get data (Opportunity, Account, etc) from Microsoft Dynamic CRM to Salesforce CRM. I will demonstrate this in the 2nd part of this article.

Salesforce ~ Microsoft Dynamic CRM Integration Part 2

Thanks

Arun Kumar

Arun Kumar

Arun Kumar is a Salesforce Certified Platform Developer I with over 7+ years of experience working on the Salesforce platform. He specializes in developing custom applications, integrations, and reports to help customers streamline their business processes. Arun is passionate about helping businesses leverage the power of Salesforce to achieve their goals.

This Post Has One Comment

Leave a Reply