Wednesday, September 16, 2020

Call Azure Function from Dynamics 365 via Webhook – Part 5.3

Register Azure Function as a Webhook in Plugin Registration Tool 

Webhooks are user defined HTTP endpoints or endpoint URL that are directly triggered by some events.

This part will focus on-

·        How to register Azure Function as a Webhook in Plugin Registration Tool?

·        How to get the Execution Context in Azure Function ?

       This is the same Execution Context that we get in Plugin when we perform CRUD operation in Dynamics CRM.

Solution

We will create Test Entity and add a field Request (Single of Text) on the form.

We will register a Webhook in Plugin registration Tool and a Step on Create of Test Entity Record.

We will check the Azure Function Log and check the presence of Execution Context in it.

We will use the same Azure Function that we have created in Part 2 - Create Azure Function using Visual Studio 

Step 1 – Go to Azure Portal https://portal.azure.com/ and open Function App that we created in Part 2 - Create Azure Function using Visual Studio

Step 2 – Click on “Functions” and then Azure Function.

Step 3 – Click on “Code + Test” and then “Get function URL” to copy the End Point URL
This URL will be used to register Azure Function as a Webhook in Plugin Registration Tool.

Step 4 – Open Plugin Registration Tool and sign-in to target environment.

Step 5 – Click on “Register” and then on “Register New Web Hook”.

Step 6 – Fill all the details as below. Click “Save” button.

Step 7 – After Endpoint URL registration, click right-click on it and create a new Step.

Note: If you are also using same entity on which we registered a plugin in previous Part 5.1 -  Call Azure Function from Dynamics 365 via Plugin, then disable that step for clear understanding. 

Step 8 - Here we will use Test Entity and register a step on Create of Test Entity record (Post Operation, Sync). You can also use Async.

Step 9 - To test the presence of Execution Context in Log, we will do a small change in Azure Function created in Part 2 - Create Azure Function using Visual Studio.
Open the Azure Function in Visual Studio, add and comment out lines accordingly.
Publish Azure Function.

Note: No need to update anything in Plugin Registration Tool as we do in Plugin each time after we update it.

Step 10 – Go to your Test Entity and make a new record.

Note: Don’t expect any updates back to this record after creation as we have not created IOrganization Service in the Azure Function code. We are focused to understand the Execution Context that we get by registering Azure Function as a Webhook.

We will update the Dynamics CRM record via Azure Function in the coming next Part.

Step 11 – To test the Execution Context coming from Dynamics CRM, we will open the log in the Azure Portal, generated after the record is created.

 Go to your Azure Function and click “Monitor”.

Step 12– Below is the Log that is generated after we created the test entity record. This means Azure Function triggered successfully.

Step 13 – Open the Log and find the Execution Context

Whoooooo!!! We did it. :)

Happy Learning, LET'S SHARE !




Tuesday, September 15, 2020

Call Azure Function from Dynamics 365 via Power Automate - Part 5.2

Power Automate

Power Automate, earlier known as Microsoft Flow has multiple connectors that perform some Actions on some Events. The combination of connectors used to create Automated Workflows.

Connector is the tool to connect two external Applications together.

Example- When  creating any record in MS Dynamics 365, insert same record into SQL Database.

MS Dynamics 365 and SQL Database are two different Applications.

Event can be Create of Record, Update of Record, Delete of Record and etc.

Action can be Create of Record, Retrieve of Record and etc.

Scenario-

A requirement states that we need to call an external API that return some response.

This API will be triggered on create of a record in Dynamics 365 and returned response will be updated back to that particular record.

 

Solution-

We can make External Http Request using Power Automate Connectors.

Step 1 – Go the https://flow.microsoft.com/en-us/ , sign-in and select Environment.

Step 2 – Go to the Solutions, Open the solution and create a new Flow.

After clicking solutions, we will find the same solutions list that we had made in our Dynamics 365 environment. We need to open any solution and create a flow under that solution just like any other component i.e. Entity, Fields, Web resources etc.

Step 3 – Search for Common Data Service Connector.

Note: Do not use Dynamics 365 connectors as they are deprecated now.

Step 4Select Trigger condition and Entity name and then click on +New step.


Step 5 – Search for HTTP.
Http is used to call API Endpoint by sending Request.

Step 6 – Follow as per below.

Step 7 – Click on +New Step to add another connector and search for Initialize.

Initialize is the connector used to contain the Response from HTTP connector (which sends HTTP Request and receive Response back).

Step 8 – Follow as per below.

This variable containing Response will be used to update the Dynamics 365 record of Test Entity.

Step 9 - Click on +New Step to add another connector and search for Common Data Service.

Step 10 – Follow as per below and Click Show advanced options.

The Unique identifier is like GUID.
Example -

Step 11 – Follow as per below and Save the flow.

After saving the flow, it will look like-

Step 12 – Now go to the Test Entity and create a New Record.

Step 13 – Save the form and reload it.
Hurrraayyyyy! We did it :)

We ran our flow successfully. To see the flow run, go the solution where we had made a Flow  and open it.

We will find the history of Flow runs.

After opening the above flow run history, we will find a successful execution of each connector.




Happy Learning, LET'S SHARE !

Call Azure Function from Dynamics 365 via Plugin– Part 5.1

Previous Post - Call Azure Function using Console Application – Part 4

Note - This blog will not focus on plugin creation.

Pre-Requisite -  

i) Create an entity named "Test" with 2 columns as below.
ii) Create and register plugin on entity "create" event  

Plugin triggered on record creation will send the request to the Azure Function and the response will be updated on the source record.


Step 1 – Create a Class Library (.Net Framework) Project from your Visual Studio.

Add a class “StudentDetail” and data member “name”. This data member will contain the request from Target (Test Entity record).

Step 2 – Add a method “CallingAzureFunction” to “DemoClass”

Step 3 – Add below code to the “Execute” method of plugin named “DemoClass”.
    

Step 4 – Register Plugin on "create" event as below
    Step 
Note: As we have used external assembly "Newtonsoft.json", to register plugin assembly in Sandbox Mode will require DLL Merge.
 

Step 5 – After Successful registration of step, go to “Test" entity and create record.

Once record saved, response from Azure Function is updated in the "Response" field.

Lets have a look at Azure Function to understand the response updated in CRM.


Happy Learning, LET'S SHARE !

D365 Plugin : Merge External Assemblies using IL Merge

Due to the SANDBOX isolation mode in all D365 CRM online versions, registering an external assembly will give an error.

This error will occur when we trigger an Event to which our Plugin using external DLL is registered.

Scenario-

From Plugin, we need to call an API Endpoint, send request and receive response against it. 

Before sending the request parameter we need to serialize the class object and after receiving response deserialize the response object, for which we used Newtonsoft.json 

As Newtonsoft.json is an external assembly we need to merge it with plugin assembly before registering it in Plugin Registration Tool.

Refer –  Call Azure Function from Dynamics 365 via Plugin – Part 5.1

Follow the below steps to merge the same.

1. Open Plugin in Visual Studio and add an external assembly.
2. Open NuGet Package Manager.
3. Search ILMerge and Install it.

Note: Internal Assemblies need to be excluded from merge to keep size of assembly less, like-

Microsoft.Xrm.Sdk.dll

Microsoft.Crm.Sdk.Proxy.dll

Microsoft.IdentityModel.dll

4. Go the properties of such assemblies and set Copy Local to False.
5. Rebuild the solution.

Below is the merged Assembly.

Note: In case when we will not exclude assemblies like Microsoft.Xrm.Sdk.dll from merging, the merged assembly size will be way more than actual-

Whoooohaa! We did it.

Happy Learning, LET'S SHARE !

Friday, September 4, 2020

Call Azure Function using Console Application – Part 4

Previous Post - Calling Azure Function using Postman – Part 3

In this part, we will create a Console Application (.Net Framework) and call Azure Function from it.

Step 1 – Open Visual Studio and create Console Application (.Net Framework) project.

 

Step 2 – Add a class “StudentDetail”and its data members as below.

We will be using these data members to send request to the Azure Function.





















Step 3 – We will "set" static values to data members of the Student Details class. 

Add below code to your Program class.


















Step 4 – Create "WebClient" to call Azure Function.

We will use “WebClient” method for sending request and receiving response and “Newtonsoft Json” library to convert student object to json string that will be further used as request parameter to Azure Function.

Add below code to your “Program” class.

Step 5Debug code line by line and see the request and response.

Request










Response








The reason why we are getting this response is because of below code in our Azure function that we had deployed.



Yay!! We Did it.

Stay Connected, to see next part of the series.

Reference to MS Documentation - 

https://docs.microsoft.com/en-us/azure/azure-functions/

Happy Learning, LET'S SHARE !

Popular Posts