To fully embrace Microsoft release cycle of Business Central, you need to start considering how you designing your solutions going forward, more precisely how much of your current functionality can be moved out of Business Central and into other products, because the simpler your Business Central codebase is, it will not only make it run faster but also make it simpler to upgrade. One of the tools that you have available to help you to move your code out of Business Central is Azure Functions. 😁

By implementing Azure Functions you can truly embrace the cloud and the serverless architecture. By using an Azure Function you have the ability to create for example a web service without having to first set up a server, installing an IIS and opening ports, 😌 all of this is handled for you with the power of one click. On top of that one of the great things about Azure Functions are, that they live in Azure so you can seamlessly integrate to pretty much any service within the Azure ecosystem, without having to do a lot of OAuth and other login procedures. 🤩

Another great thing about moving your code to Azure Functions is that it will also help you to decouple your code, making it easy to change your code without having to worry about breaking changes. It also allows you to share your code across multiple systems, this means as a partner you have the ability to start selling your functionality as a SaaS, and as an end-user, this means that if you, for example, want some kind of calculation in both your CRM and Business Central, instead of either having to overload one of your systems by acting as a backend or even worse coding the same solution in both systems having to maintain both 😴. You can just place the calculation in an Azure Function and then calling that function from both systems, meaning you only have one codebase to maintain. The possibilities are endless when it comes to using Azure Functions, however, many Business Central developers might fear using them, because it is something “new” well in this post I will try to guide you through the process of creating your own Azure Function, however, I will be using C# so if you do not know how to write C# some of the code might be hard to understand, but I will keep it as simple as possible, and as always you can find all the source code here. https://dfredborg.visualstudio.com/Business%20Central/_git/Business%20Central?version=GBMyAzureFunctions

To get started with creating your own Azure Functions, you have two possibilities the first is that you can create your Azure Function directly from the Azure portal https://portal.azure.com/ by going to Azure Functions and click add and go through the guide. The second option, which is the one that I prefer and the one we will cover in this post, is creating your Azure Functions through Visual Studio. The reason why I prefer to use Visual Studio is that it allows you first of all to use arguably one of the greatest IDE’s out there 😉, and also it lets you run your Azure Functions locally in a simulator, so you can test it locally before uploading. 

The first thing you must do is download and install Visual Studio https://visualstudio.microsoft.com/downloads/ here you can just go with the free Community edition, doing the installation you will be given the choice of which capabilities that you wish to add to your Visual Studio, here you must select Azure Functions. Once everything is installed you are ready to create your first Azure Function. When you launch Visual Studio you will be met with a page where you can choose to create a new project. 

Click this and choose Azure Functions from list. 

Give it a project name and a solution name and choose the location. 

Once you click create you will be given a list of different templates to help you get started, now I will not go into these different templates, you just need to know that they are there, so if you ever feel like there is something you need to do, chances are that Microsoft has created a template to help you get started, for the purpose of this post we will choose the Http trigger, what this basically means is that we want to create an HTTP web service. 

On the left you can choose what type of Authorization level that you want for you function, you have anonymous which means anyone can call your function, admin which means that you must have the master key, and lastly Function which means that it will be token-based, here you should choose Function and click create.  

This will generate a very simple HTTPTriger function for you the first thing you will notice is that you FunctionName is set to Function1.  

[FunctionName("HelloWorld")] 

You should change this to something more correct, to keep it simple in our case let us just rename it to HelloWorld 

[FunctionName("HelloWorld")] 

the next line. 

[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] 

This is where you define that this should be triggered by an Http Call, your Authorization Level, and what kind of request you will support, wherein our case we support both get and post requests. Before we get into the rest of the code let us try to run our function and see what it does, to run your code press F5, this will start a command prompt which will emulate your Azure Function, where the last line will give you a URL that you can use to call your function. 

If you copy this URL into you browser you should see something like this. 

And if you view the command prompt, you can see what the Azure Function does, now try to add ?name=Jack to you URL so your URL should be something like this http://localhost:7071/api/HelloWorld?name=Jack and this should return you something like this. 

Congratulation you have now created a web service 😆, let us break down the rest of the code. 

log.LogInformation("C# HTTP trigger function processed a request.");  

This line writes a message to your log. 

string name = req.Query["name"]; 

This is where you retrieve the value that you entered in the ?name part of your URL, you can add as many req.Query that you want, allowing the user to parse information to your function. 

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody); 
            name = name ?? data?.name; 

This part is a little bit more complicated, but simply put all it does is that check ´s if you have sent a JSON body, which it then adds to a dynamic object, where it checks if there is a name in the object. 

The next part: 

string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully."; 
            return new OkObjectResult(responseMessage); 

This checks if the name was entered and then returns the result in a OkObjectResult. 

As you can see then this is very simple, and if you know how to write C# then the possibilities are endless, you could use it to integrate with other systems, upload files to an FTP Server, or pretty much anything. Once you have created your awesome Azure Function, you can deploy it to Azure within Visual Studio, all you have to do is go to Build->Publish … 

This will then based on your login, allow you to create a new function in the Azure Portal which you can then integrate to. 

Well that was it for this post, in the next post we will see how you can integrate with your Azure Function from within Business Central. But until next time stay safe. 😷

Leave a Reply