MuleSoft and Microsoft Dynamics 365 (CRM) — Part 2

Zoran Đukić
Another Integration Blog
5 min readNov 21, 2023

In the last installment, we talked about how to set Dynamics CRM in MuleSoft: https://medium.com/another-integration-blog/connecting-mulesoft-and-dynamics365-crm-365d26a35eac

In this installment, we are going to continue working on our Dynamics 365 and focus on fetching information from CRM by using Mule and Postman. Let’s start by importing Dynamics to our Mule environment.

Microsoft Dynamics CRM

This is the dependency:

  <dependency>
<groupId>com.mulesoft.connectors</groupId>
<artifactId>mule-microsoft-dynamics365-connector</artifactId>
<version>2.6.1</version>
<classifier>mule-plugin</classifier>
</dependency>

After we import the dependency, you need to test the connection. Enter your credentials and if they are successful you can just leave it for now, if you are not successful you can check the link in the beginning and start again.

Note: We are going to focus a little bit less on MuleSoft and a little bit more on how to find the data we need to work with MuleSoft.

First, let’s try to create an Account. Try to populate as many fields as possible. Why? So later on you can identify which field is which. I usually just write the name of the field as a value.

Note: If possible, try to do this exercise with a fresh account. Admins can change some of the things we are about to do and they may not work.

CRM

Now, we have two ways we can get the information from Dynamics: Postman and Mule.

First, let’s try with Postman.

Note: You have to enable your account to use Postman in Dynamics. Instructions are in the first part of this series. If you get 403 errors, the problem is in your Dynamics Manifesto. If you get a 401 error, you have a problem with id, secret or token.

Before we start we need a token. Create the following request:

GET https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token

Body (x-www-form-urlencoded):
client_id: your application/client id
client_secret: client secret you generated in Azure
grant_type: client_credentials (hardcoded value)
scope: https://your-crm-name.crm4.dynamics.com/.default

And the response should look something like this:

Example of token

Great! Now, let’s fetch account data.

Create a request:

GET https://your-org.crm4.dynamics.com/api/data/v9.2/accounts

Headers:
Authorization: Bearer + your token

Your response should look something like this:

/GET account response

Now let’s use the info we get here to try out our Mule implementation. First, when created you can see ID in the URL. For example.

https://your-org.crm4.dynamics.com/main.aspx?appid=4e5d1a45-1386-ee11-8179-00224882b633&pagetype=entityrecord&etn=account&id=1f3443e6-4588-ee11-8178-6045bda16033

Second way is to find your account in /GET all accounts response. Now, lets call Haaland’s account from Mule.

Retrieve Account from Dynamics CRM

XML:

<flow name="get-account-by-id-flow" doc:id="e12679f2-2bcd-46ca-b683-332e6470de64" >
<scheduler doc:name="Trigger every 5 min" doc:id="a6914193-41e5-4ba5-b2ad-2dcef3d276c2" >
<scheduling-strategy >
<fixed-frequency frequency="5" startDelay="5" timeUnit="HOURS"/>
</scheduling-strategy>
</scheduler>
<dynamics:retrieve logicalName="account" doc:name="Retrieve Account" doc:id="c4473f4a-2c75-4fb7-bef7-5db8e8b4b1ae" config-ref="Dynamics_365_Dynamics_365">
<dynamics:id ><![CDATA[#["1f3443e6-4588-ee11-8178-6045bda16033"]]]></dynamics:id>
</dynamics:retrieve>
<ee:transform doc:name="Transfom to JSON" doc:id="8504a933-6b33-43e3-868c-d75ab8a6fb03">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="INFO" doc:name="Log Payload" doc:id="eef0aea7-81ea-4b7f-8dbc-8504f93d8cbf" message="#[payload]" />
</flow>

Response:

{
"address2_utcoffset": null,
"address1_city": "City",
"address2_name": null,
"address1_latitude": null,
"address1_addresstypecode": null,
"_originatingleadid_value": null,
"openrevenue": 0.0,
"participatesinworkflow": false,
"emailaddress2": null,
"emailaddress3": null,
"emailaddress1": null,
"statuscode": 1,
"accountid": "1f3443e6-4588-ee11-8178-6045bda16033",
"_slaid_value": null,
"entityimage_url": null,
"opendeals": 0,
"shippingmethodcode": 1,
"address1_line1": "Street 1",
"_owningteam_value": null,
"donotsendmm": false,
"_msdyn_accountkpiid_value": null,
"address1_shippingmethodcode": null,
"fax": null,
"adx_createdbyipaddress": null,
"_modifiedonbehalfby_value": null,
"address1_line3": null,
"address1_line2": "Street 2",
"address1_longitude": null,
"address2_fax": null,
"primarysatoriid": null,
"preferredappointmenttimecode": null,
"address1_utcoffset": null,
"timezoneruleversionnumber": null,
"address1_stateorprovince": null,
"stockexchange": null,
"adx_modifiedbyipaddress": null,
"address2_city": null,
"address2_postalcode": null,
"modifiedon": "2023-11-21T08:13:50Z",
"lastonholdtime": null,
"address1_upszone": null,
"donotphone": false,
"address2_latitude": null,
"industrycode": null,
"timespentbymeonemailandmeetings": null,
"donotbulkpostalmail": false,
"customersizecode": 1,
"marketcap": null,
"customertypecode": null,
"territorycode": 1,
"address2_addressid": "0dfa214b-75c0-47bc-801e-1fca923322ce",
"utcconversiontimezonecode": null,
"msdyn_gdproptout": false,
"statecode": 0,
"address1_postalcode": "Postal code",
"aging60": null,
"_territoryid_value": null,
"openrevenue_state": 1,
"aging30_base": null,
"sharesoutstanding": null,
"_owningbusinessunit_value": "1c3b1ba7-8b85-ee11-8179-6045bd9055b8",
"overriddencreatedon": null,
"_msa_managingpartnerid_value": null,
"address2_county": null,
"address2_shippingmethodcode": 1,
"entityimageid": null,
"address2_primarycontactname": null,
"donotfax": false,
"businesstypecode": 1,
"_preferredserviceid_value": null,
"versionnumber": 11376954,
"address1_primarycontactname": null,
"createdon": "2023-11-21T08:13:50Z",
"address2_telephone2": null,
"address2_telephone1": null,
"_modifiedby_value": "0e421ba7-8b85-ee11-8179-6045bd9055b8",
"address2_telephone3": null,
"address2_line3": null,
"address2_postofficebox": null,
"processid": null,
"aging90_base": null,
"websiteurl": null,
"address2_line1": null,
"_slainvokedid_value": null,
"address2_line2": null,
"lastusedincampaign": null,
"onholdtime": null,
"marketingonly": false,
"_ownerid_value": "0e421ba7-8b85-ee11-8179-6045bd9055b8",
"address1_name": null,
"stageid": null,
"address1_telephone2": null,
"yominame": null,
"importsequencenumber": null,
"address1_telephone3": null,
"telephone3": null,
"telephone2": null,
"telephone1": "0956951234",
"_createdbyexternalparty_value": null,
"ownershipcode": null,
"_primarycontactid_value": "d1bf9a01-b056-e711-abaa-00155d701c02",
"creditlimit_base": null,
"donotemail": false,
"revenue": 1.5E7,
"opendeals_date": "2023-11-21T08:13:51Z",
"address1_postofficebox": null,
"address1_country": null,
"address2_stateorprovince": null,
"address1_composite": "Street 1\r\nStreet 2\r\nCity Postal code",
"address1_telephone1": null,
"aging30": null,
"openrevenue_base": null,
"donotbulkemail": false,
"address1_freighttermscode": null,
"donotpostalmail": false,
"address1_addressid": "86b55036-fc01-4492-93be-a5bd58e68a0d",
"preferredcontactmethodcode": 1,
"_modifiedbyexternalparty_value": null,
"address1_fax": null,
"entityimage": null,
"_preferredequipmentid_value": null,
"_parentaccountid_value": null,
"name": "Erling Haaland",
"paymenttermscode": null,
"_masterid_value": null,
"accountratingcode": 1,
"preferredappointmentdaycode": null,
"address2_composite": null,
"marketcap_base": null,
"creditonhold": false,
"openrevenue_date": "2023-11-21T08:13:51Z",
"exchangerate": 1.0,
"entityimage_timestamp": null,
"accountclassificationcode": 1,
"description": "This is a description",
"primarytwitterid": null,
"numberofemployees": 12,
"sic": null,
"opendeals_state": 1,
"_msdyn_salesaccelerationinsightid_value": null,
"ftpsiteurl": null,
"address2_addresstypecode": 1,
"accountcategorycode": null,
"_createdby_value": "0e421ba7-8b85-ee11-8179-6045bd9055b8",
"teamsfollowed": null,
"adx_createdbyusername": null,
"address1_county": null,
"address2_country": null,
"_createdonbehalfby_value": null,
"tickersymbol": null,
"address2_freighttermscode": 1,
"accountnumber": null,
"_owninguser_value": "0e421ba7-8b85-ee11-8179-6045bd9055b8",
"_defaultpricelevelid_value": null,
"adx_modifiedbyusername": null,
"merged": false,
"aging90": null,
"_transactioncurrencyid_value": "45affcb5-0d86-ee11-8179-6045bd9055b8",
"traversedpath": null,
"_preferredsystemuserid_value": null,
"aging60_base": null,
"followemail": true,
"revenue_base": 1.5E7,
"address2_longitude": null,
"address2_upszone": null,
"creditlimit": null
}

This is it folks. Now, you know how to get data from Dynamics CRM. In the next installment, we are going to focus on creating, updating, and deleting the data from our CRM.

--

--