Invoking Azure Functions to replace DOT NET calls in C/AL or AL

Standard

Recently Microsoft announced that dotnet can still be used with installations on premise of Dynamics 365 Business Central.

However, if our extension is to make it in the cloud the code leveraging dot net needs to be replaced with http api calls.

In this example I will show how a legacy C/AL code using dot net can be replaced with a call to an Azure function to achieve the original goal of validating a posting code.

Premise

  • Either Table 18 was modified and additional code was added in “Post Code” Validate trigger with Regex class entities to perform validation on post codes.
  • Or, the additional validation is executed when the Post Code Validate in standard is finished and a subscriber to Post Code Validate exists in our extension and is triggered, but still contains dot net code(RegularExpressions class entitites) as we’re only dealing with on-premise (target=internal in app.json)

Objective

I want the additional validation to be executed when the standard validation is finished and the additional validation to not contain dotnet calls.

Design

  1. In a new AL project add a new codeunit:

add_al_codeunit

2. The codeunit itself contains an event subscriber to Table18.Validate.PostCode.

(Use “teventsub” snippet to get the quick scaffolding of the event subscriber)

codeunit_content

When the subscriber is triggered we are executing an Azure Function call: azfnvalidate-us-ca-zipcode. We’re retrieving a json object whose content is : {“PCValid” : true} or {“PCValid” : false}.

3. Write the Azure Function with Visual Studio Code

Pre-requisites:

  • Azure subscription
  • install C# extension
  • Azure Function Core Tools extension
  • install .net core (use npm or chocolatey)
  • Azure CLI Tools

VSCodeExtensions

A good guide to get you started with Azure Functions is here.

Once you have the default “Hello World” Azure Function created, replace your Run function with:

azFn

Publishing the function in Azure should generate a record in your chosen storage:AzureFninPortal

Testing

  1. Once published we can quickly spin a test for the new Azure Function in a web browser window:

web_browser_test

2. Removing the “W” in the previous test, triggers the Azure Function to return above json.

web_browser_invalid_postcode

3. Let’s test now the validation in Business Central:

ezgif-3-34f9ae149c11

Therefore, to replace a set of dotnet calls we need a worker placed somewhere else other than in AL or C/AL and a caller of that worker services placed in the extension. In my example use a codeunit (caller) in the extension range with a subscriber event defined that calls an Azure function(worker).

What other methods are you employing to achieve similar results ?

If you liked this article bookmark my blog or follow me for more stuff about NAV and Business Central.

Dynamics 365 Business Central : Al Code Analyzers #TurnOnCops #Extensions #Permissions

Standard

When you turn On PerTenantExtensionCop in Visual Studio Code and you forgot to create a Permissions.xml file, you get a compile error in your extension.

In Visual Studio Code – > User Settings add an entry for al.codeAnalyzers token like below:

CodeCops

Without this flag turned on your extension compiles.

If you do set PerTenantExtensionCop and if you’re missing Permissions.xml, you’re going to get a compile error:

Compile Error

By Adding in the project root a file Permissions.xml the error is solved:

Success

More reading on code analyzers here.

Original post here.

Dynamics 365 Business Central : Extending Role Center headline with web service data, lists and dictionaries

Standard

So much to read, so little time … the speed at which Microsoft adds new Business Central and AL features is overwhelming 🙂

In this blog I’ll demonstrate how I was able to display weather temperature for 3 cities in Business Central role center headline.

ezgif-3-ef3a02f8f885

First, there are 9 headline role center pages in Business Central, with ID from 1440..1448.

headline pages

I will extend the headline role center for the page 1440 : “Headline RC Business Manager”, by adding three fields, one for each city and its temperature.

To record the three cities and their temperatures I am using here a list and a dictionary data structure.

Fields_And_DataStructures

This is followed by a querying of a weather web service and recording of the 3 cities and their temperatures in a dictionary:

Query Web Service

Commented is the response from the web service.

I need data stored in the following tokens:

  • $main.temp
  • $sys.country
  • name

For more info on how to parse web service response take a look at Mr. Kauffman blog.

json

I use a free web service for weather openweathermap. You need to create an account and you will get a free APPID when you complete the registration. You can only query the web service once every 10 minutes for the same location.

Finally, to load cities and their temperatures in your headline use the code below:

CityTemp

The complete pageextension object is included here.

That’s it … thanks for reading!

Original post here.