We have a scenario where we want to call a third party API on update of a field on account entity in Dynamics 365, send some data to that API, then update Dynamics.
To achieve above we will be using azure function to trigger on update of that field in Dynamics and call/register that function under webhook. In this example we will only be updating Dynamics account entity in Function just to prove the point but can easily call API or can carry on any other functionality.
First step is to create a new project in Visual Studio 2017 of type Azure Functions (You will need to install Azure SDK if you have not done)
On next screen select Http trigger as we will be triggering this via webhook
By default you will get following Function class and project structure
Let’s delete or rename Function1.cs to FunctionCallExternalAPI.cs
Now add few nuget packages for D365 calls
Install-Package Microsoft.CrmSdk.CoreAssemblies
Install-Package Microsoft.CrmSdk.XrmTooling.CoreAssembly
Now add a new folder under your project and call it Helper (just to separate code) and we will add a new class and call it Common.cs
This class has two methods
GetD365Context – this takes JSON content and return plugin (webhook) execution context
GetEnvironmentVariables – this takes name as parameter and returns results ( we will add connection string later to this function)
Add following code to FunctionCallExternalAPI.cs
Let’s add FunctionProcess.cs class which we will use to make decision what to do with the context
In our PostAccountUpdate we will simply update name of account with country being updated on account
our code is complete now.
Let’s go update application settings and then publish function
Once you have published you will see settings like below
Click on Manage Application Settings, this is where we will store our D365 connection String
Click a new entry and give it a name ‘Dynamics’
Add your connection strings as needed, something like below
“AuthType=Office365;[email protected]; Password=password123;Url=https://rokhritest.crm4.dynamics.com”
Publish again
Now login to azure portal, get function URL from screen below
Now go to plugin registration tool and register a new webhook
Make sure Endpoint URL does’t have code parameter in it so it will be like below
https://azurefunctionrokhri.azurewebsites.net/api/FunctionCallExternalAPI
and then select WebhookKey and enter key from your URL
Then add a step under function ( as you will do for a plugin )
Job done. Now when you will update account it will call function and you can do what want to do (currently only updating D365 account entity but you might want to call an API, something else etc.)