Azure Function for Dynamics 365 CRM: Part 3 [Website Contact Us Integration]

Amit Prajapati
7 min readMar 8, 2021

--

In the previous blog, we have learned how to create an Azure function that connects to Dynamics 365 and deploy it to the cloud. Today, we are going to create Azure Function for Website Contact us form and the following is the use case:

When the user submits the Contact us of your Website with the details i.e. First Name, Last Name, Company name, Email Address, Case name & Case Description. Account, Contact, and Case entity records will be created in Dynamics 365 CRM.

In this way, you can integrate your Website Contact us form with Dynamics 365 for Case creation using Azure Function.

Step 1: Create an Azure Function to Create a record Account, Contact, and Case Record in Dynamics 365 CRM

First, we will create a Function App Project in visual studio, and the following is a reference blog link for the same:

Let’s consider, the following is payload data that will be posted from the Contact Us to Azure Function.

We will create a DTO file for the Response message to serialize the data into DTO. For that, we will Right-click on the Project and add the folder named “DTO” for best practices.

Add a new Folder in Visual Studio Project

Let’s create a Class file under the DTO folder.

Add a Class in Visual Studio Project

Copy the sample response and open the newly created class file under the DTO folder. Go to Edit and do a Paste Special as “Paste JSON as Classes”

Paste JSON as Classes using Paste Special

It will generate the class with {get; set;} variables mention in the below screenshot. Now, remove the RootObject class.

Before:

Remove the RootObject class

After:

After removing RootObject class

We will update the Azure Function code to get the response and serialize the data into DTO using the following code.

In the previous blog, we have created a Connection method here we going to use the same method to connect to dynamics 365 CRM. The method will return the Dynamics 365 CRM service if a connection is established. Please refer to the previous blog for that.

Here, we will use the existing entity from the Dynamics 365 CRM to create the records. As we receiving the first name, last name, company name, email address, case title, and case description. Below is the flow of how records will be created in Dynamics365.

1. Account will create with Company name from the responses.

2. Contact will be created with First name, last name, and email address. Also, we will as a newly created account as a company of contact.

3. Case will be created with title and description. We will also add the customer name Account and Contact Entity.

Following is the code for the above logic:

Now, we will return the response from the Azure Function based on the success and failure.

Full SendResponse Code:

Step 2: Testing on Local Machine using Postman

We will require the API testing tool, here I am using Postman and the following is the link to download “Postman”. https://www.postman.com/downloads/

To test the application, click on the Start button on top of Navbar as mentioned below in the screenshot [Button will have Project Name]. It will take a few minutes to Load the Azure Emulator

Click on Run

Following is the screen you will be able to see and copy the URL highlighted in the red below and paste that URL in Postman.

Open Postman and Select request as POST and paste the URL then click on send:

You will get the response based on the return value:

Following is a screenshot of Dynamics 365 CRM with Case records created using Azure Function [Request send from Postman]

Step 3: Deploy Azure Function to Cloud

Please refer to my previous blog on Cloud Development of Azure Function for more details:

Right-click on the Project and click on the Publish button:

Click on the Publish as Azure Function App profile is already created in the previous blog.

After deploying Azure Function, login to the Azure portal and open the Azure Function to get the URL:

Step 4: Update Azure Function URL in the website

On your website, you can use the azure function URL as your endpoint to send data from your contact us page. Based on your website configuration may be your backend can be in PHP, Vanilla js, or Nodejs you can update the URL accordingly.

Result:

Following is the website that I have created for this Demo purpose:

When you click on send, it will trigger the deployed azure function that we process the response and post the data to Dynamics 365 CRM:

CORS Issue after deployment and integration

After you deploy the azure function and integrating it with your Contact Us form it may happen that your integration will not work. The reason for this error is due to your website and azure function may be on different domains and you will get the following error in your console of browser:

To resolve that Go to the Azure portal and Open the Azure Function App. Search for the CORS option in the left navigation

After clicking on the option you will get the following screen in Azure Portal where you need to add your website from which the Azure Function app will be triggered. So that the Azure Function app will allow the request from your website. It also the most important feature that all the servers and modern browsers provide to avoid any malicious activity.

Add your website and click on Save.

Now, your Azure Function App will allow the request from your website. It is also the best feature because if any have your azure function your but you have not allowed those URLs then no one can trigger the azure function directly.

Github Link of Contact Us Code:

Live Contact Us:

Please do clap and follow me for more blogs on Azure Function. In Case of queries regarding Azure Integration with Dynamics 365 connect with me on LinkedIn

--

--

Amit Prajapati

I am Technical Consultant from Mumbai, India. I like to contribute my knowledge to the community and good social cause.