Recent Posts

Archive

Tags

Scale Out NAV Data using Azure Cosmos DB

Integrating with NAV is easy and Microsoft have done a great job in providing multiple ways of doing so.

There may come a time when integrating with an external application becomes so intensive that using the standard methods are not optimal. Scaling Up your instance is one way, but this can become a costly exercise. Another way is to provide your integration data to Cosmos DB where it can easily Scale Out providing better global performance and latency with less overhead on your NAV instance.

This blog will show you how to provide your data to Azure Cosmos DB from NAV.

Please Note: Any coding below is just for demonstration. It is by no means something that is for a live environment.

Scenario:

Your customer is complaining that the performance of their website that requests data from NAV is slow. You decide to use Cosmos DB to store item and pricing information. This way the website can request data from multiple regions around the world where the web site is running.

Requirements:

To get things up and running you’ll need the following:

· An Azure Subscription

· Microsoft Dynamics NAV 2018 with the Modern Development Environment ready to go.

Creating the Cosmos DB:

https://docs.microsoft.com/en-us/azure/cosmos-db/

Azure Cosmos DB is a globally distributed, multi-model database service.

1. In your Azure Portal create a new Cosmos DB Account.

2. Access the quick start section to create your collections. This is will also give you sample applications in different languages to view the information using .Net, Xamarin, Jana, Node.js and Python.

The quick start should create for you a new cosmos database called ToDoList and a collection called Items. See the picture below:

Azure Functions

The next step is to create an Azure function to process requests that will come from NAV and then put them into Cosmos DB. This was by far the easiest way I found to get the data into Cosmos DB by NAV just invoking the function.

Here is a reference on how to get up and running with storing unstructured data using Azure Functions and Azure Cosmos DB. https://docs.microsoft.com/en-za/azure/azure-functions/functions-integrate-store-unstructured-data-cosmosdb

1. Create a new Azure Function. This must be of type HttpTrigger.

On the integrate tab add an additional output to Azure Cosmos DB. It should look something like the image below:

3. Ensure that Database name is ToDoList and the collection name is Items.

4. The Azure Cosmos DB account connection provides the link to the cosmos database you may have created earlier. Click on new and follow the prompts to populate this field.

5. Once the output has been specified you now need to place code into the Azure Function itself. I’ve provided a sample of this code below:

6. Next is to click on the Get function URL to copy the information. Save it somewhere as you’ll need it for NAV.

The NAV code:

The code below creates a simple codeunit that calls the Azure Function created in the previous section.

You should be able to copy and paste the code into a new AL project, run it and see the results come through to Cosmos DB.

/// Sample repository codeunit to send data to CosmosDB

/// Reference: https://docs.microsoft.com/en-za/azure/azure-functions/functions-integrate-store-unstructured-data-cosmosdb

codeunit 50100 CosmosDBRepository

{

Description = 'Azure Cosmos DB Repository';

var

_azureFunctionURL: TextConst ENU='https://<YourAzureFunctionApp>.azurewebsites.net/api/HttpTriggerCSharp_SendProductToCosmosDB';

/// This procedure sends item information to CosmosDB

local procedure SaveItemToDocumentDB(itemNo: Code[20]);

var

item: Record Item;

httpClient: HttpClient;

httpResponseMessage: HttpResponseMessage;

begin

if item.get(itemNo) = false then exit;

// Populate headers with information you want to save to DocumentDB

with httpClient.DefaultRequestHeaders do begin

Add('id',item."No."); // This id is important as it will give the document a unique id

// if nothing is specified one will be automatically created as a GUID

// if you send the id more than once it will just overwrite the previous document :)

Add('name',item.Description);

Add('uom',item."Sales Unit of Measure");

Add('price',format(item."Unit Price"));

end;

// Invoke the Azure Function

if httpClient.get(_azureFunctionURL,httpResponseMessage) = false then

Message('oops!\ something went wrong.\\%1 %2',httpResponseMessage.HttpStatusCode,httpResponseMessage.ReasonPhrase);

end;

// This is just code to test when you compile and run.

[EventSubscriber(ObjectType::Codeunit,1,'OnAfterCompanyOpen','',false,false)]

local procedure ExecuteSaveItemsToDocumentDB();

var

items: Record item;

iCounter: Integer;

begin

repeat

SaveItemToDocumentDB(items."No.");

iCounter += 1;

//Please Note: You may need to sleep the thread if you are on a clow Cosmos DB plan.

//Sleep(1000

until (items.Next = 0) or (iCounter = 10); // or ten iterations only

Message('Documents have been sent to CosmosDB');

end;

}

In Summary

Technology is changing so fast and being able to incorporate this change into your development experience not only will enrich you, but will also maximize the value you can provide to customers. This blog is but just one thing that brings NAV data into Azure in a way that other applications can use on a global scale.

If you like this blog, please be sure to let me know and I’ll gladly share more.

  • Facebook
  • LinkedIn

©2020 Riverine Technology Services.