Dynamics 365 for Sales and Azure Functions

In this blog we’ll be looking at a very common scenario of creating Leads from a contact us page in Dynamics 365 for Sales using Azure Functions.

Azure Functions fit this case rather nicely as the inflow of leads is not something that can be considered heavy, especially from a Contact Us form. The serverless infrastructure and consumption-based plan make Azure Functions very attractive to be used as a solution.

Instead of having a VM running 24/7 and an integration process running to convert information from Contact Us form to Dynamics 365 Lead, we can simply convert our Create Lead function to an Azure Function and deploy to Azure. This will make sure that our function invokes only when needed.

To keep the example simple, I’m assuming input of first name, last name and e-mail address. Our Azure Function is going to be URL based, that means we can invoke our Azure Function by a URL and passing the information either as query string parameters or as part of the HTTP request body.

 

public static async Task<HttpResponseMessage>
Run([HttpTrigger(AuthorizationLevel.Function, “get”, “post”, Route = null)]HttpRequestMessage req, TraceWriter log)
{
log.Info(“C# HTTP trigger function processed a request.”); ThreadPool.SetMinThreads(10, 100); //Increase min threads available on-demand
ServicePointManager.DefaultConnectionLimit = 10000;
ServicePointManager.Expect100Continue = false;
ServicePointManager.UseNagleAlgorithm = false;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

// parse query parameter

string firstName = req.GetQueryNameValuePairs().FirstOrDefault(q => string.Compare(q.Key, “firstname”, true) == 0).Value;

string lastName = req.GetQueryNameValuePairs().FirstOrDefault(q => string.Compare(q.Key, “lastname”, true) == 0).Value;

string email = req.GetQueryNameValuePairs().FirstOrDefault(q => string.Compare(q.Key, “email”, true) == 0).Value;

// Get request body
dynamic data = await req.Content.ReadAsAsync<object>(); // Set name to query string or body data
firstName = firstName ?? data?.firstname;
lastName = lastName ?? data?.lastname;
email = email ?? data?.email; var entity = new Entity(“lead”);
entity[“firstname”] = firstName;
entity[“lastname”] = lastName;
entity[“emailaddress1”] = email; if (!string.IsNullOrEmpty(firstName))
{
ServiceManagerContext.Current.ParallelProxy.Create(new List<Entity> { entity }, new OrganizationServiceProxyOptions { Timeout = TimeSpan.FromMinutes(20.0) });
} return firstName == null
? req.CreateResponse(HttpStatusCode.BadRequest, “Please pass information to create a lead.”)
: req.CreateResponse(HttpStatusCode.OK, “Lead Created.”);
}

 

Explore Microsoft Dynamics 365 Services

Learn More

 

In our code, we are extracting first name, last name and e-mail address from either the query string parameters or the HTTP Request body. Once we have that, we create a new Entity object and set the attributes.

Next step is to create that lead in Dynamics 365. We’re using PFE xRM Core library for interacting with Dynamics 365 and we simply pass our entity as a parameter. That’s it, we have our lead created in Dynamics 365 and our Azure Function execution is complete. All the resources are released at this point.

A few things that need to be taken into consideration while working with Azure Functions is that, the task or operation being performed in Azure Function should be atomic and short running. If there’s a need for a long running job, then for that it needs to be either broken in smaller parts or Azure Logic Apps need to be considered for implementation.

 

Alphabold implements Dynamics 365 for Sales with Azure Functions

 

Alphabold, as Microsoft Dynamics Partner, works for companies and helps them in implementation of Dynamics 365 for Sales. During our implementation, Azure Functions is one of the new tools that we leverage to build cost effective, powerful integrated solutions. It’s a cost effective solution because Azure Functions is a serverless compute experience.

I hope this blog helps as a starting point in using Dynamics 365 with Azure Functions.

If you have any question or queries, do not hesitate to reach out to us!Â