Create Dynamics 365 Records From Azure Service Bus Queue – Via Logic App

ByRob Peledie

Create Dynamics 365 Records From Azure Service Bus Queue – Via Logic App

I wanted to put together a solution where an external system could send a JSON payload through an Azure Service Bus Queue, and a new record created in Dynamics 365.

There are no doubt better, or at least different ways to approach a scenario like this, but I wanted to brush up on some skills, so thought it was a nice use case, and one I will be needing soon for integrating an external Oracle system.

So, basically this is the running order of steps:

  • Create Azure Service Bus Queue
  • Create Logic  App
  • Test (Using Postman to send JSON messages)

So, nothing too complex, but just thinking about this sort of scenario a few years ago (not that many to be fair), would have required a fair amount more development skills, but pretty well all of the above can be achieved in a codeless manner. As I have said before, “Don’t use a Sledgehammer to crack a walnut”. I have also tried to add some links to useful references within this blog.

So firstly, setup an Azure Service bus, and make sure you note the Primary Connection string, Queue name, and primary key.

If you’re planning on using something like Postman to test, the you’ll need to create a SAS Key

Open up a Cloud Shell and use the following to create a SAS Key.

You’ll need the Service Bus URI and the Queue Name, as well as the policy name and Key.

[Reflection.Assembly]::LoadWithPartialName("System.Web")| out-null
$URI="myNamespace.servicebus.windows.net/myEventHub"
$Access_Policy_Name="RootManageSharedAccessKey"
$Access_Policy_Key="myPrimaryKey"
#Token expires now+300
$Expires=([DateTimeOffset]::Now.ToUnixTimeSeconds())+300
$SignatureString=[System.Web.HttpUtility]::UrlEncode($URI)+ "`n" + [string]$Expires
$HMAC = New-Object System.Security.Cryptography.HMACSHA256
$HMAC.key = [Text.Encoding]::ASCII.GetBytes($Access_Policy_Key)
$Signature = $HMAC.ComputeHash([Text.Encoding]::ASCII.GetBytes($SignatureString))
$Signature = [Convert]::ToBase64String($Signature)
$SASToken = "SharedAccessSignature sr=" + [System.Web.HttpUtility]::UrlEncode($URI) + "&sig=" + [System.Web.HttpUtility]::UrlEncode($Signature) + "&se=" + $Expires + "&skn=" + $Access_Policy_Name
$SASToken

To test the Service Bus Queue I used Postman. You can add a number of useful Postman collections for Azure from Ludvig Falck on GitHub

There’s a few steps then in Postman to setup the parameters, Headers and Body (I just setup a really simple JSON payload as you can see).

Parameters

Add queueName as a Key and the queue name as the Value

Headers

For the Headers, you’ll need the SAS key you generated earlier.

Body

For the body, I just used a really simple payload

{
"email":"me@me.com"	,
"first":"John",
"last":"Smith"
}

Once this is completed, you should be able to hit “Send” and see the message being created in the Azure Service Bus Queue

Next step is to grab that message from Service Bus, and use it to create a Record in this case.

I decided to use Logic Apps as opposed to Flow, just to keep everything in Azure (although technically it’s all in Azure, but you get what I mean).

So this is the basic flow:

It’s pretty easy to follow. When a message is received in the Queue, grab it, Parse the JSON, use the data to create a record, then complete that message in the Queue.

One thing to bear in mind is that the JSON payload will need to be decoded to then be used in the creation of a Dynamics Record. In the “Content” field of the Parse JSON action, add this expression:

decodeBase64(triggerBody()?[‘ContentData’])

You’ll also need to add the JSON Schema. For the example Payload I used this:

{
    "properties": {
        "email": {
            "type": "string"
        },
        "first": {
            "type": "string"
        },
        "last": {
            "type": "string"
        }
    },
   
}

Once this is all done, Save the Logic app and Run it. If you’ve followed all the steps, sending a JSON payload from Postman should create a record in Dynamics.

About the author

Rob Peledie administrator

1 Comment so far

Create Dynamics 365 Records From Azure Service Bus Queue – Via Logic App – 365Knowledge – Dynamics365BlogsPosted on6:34 am - Jun 5, 2019

[…] Create Dynamics 365 Records From Azure Service Bus Queue – Via Logic App […]

Leave a Reply