Introduction.

I am not the first to write about Azure functions and NAV: last year Vjekoslav wrote this great post http://vjeko.com/invoking-azure-functions-al/
So, what´s new now? The new stuff is motivation: what Azure functions and serverless gives us. The new is also sending data from NAV to azure and write an Azure queue.
Queues is another way to decouple: you write a message in the queue and later another apps can read these data messages and process then later. Not the subject of this post, but it is an interesting additional element to play with.

The beginning: trying to make my own server.

In this previous post https://community.dynamics.com/nav/b/conceptsindailynavisionwork/archive/2018/06/24/webhooks I make a NAV side webhooks with this feature:
  • Raise an event.
  • Call an external URL when event happens.
Not so important, but I strongly like to do the other side: the external server to be called.
So, I have made in my PC a JavaScript program. (It´s no important to understand this code. You can continue reading without it):
const http = require('http')
const Fichero = require('fs')
const server = http.createServer((request, response) => {
    response.writeHead(200, {'Content-Type': 'text/plain'})  
    response.end(RespuestaAlServicio(request.url));
    if (request.method == 'POST'){
        let body = '';
        var qs = require('querystring');
        request.on('data', (data) => {
            body += data;
        });
        request.on('end', function () {
            var post = qs.parse(body);
            Fichero.writeFileSync('\Prueba.txt',post);   
        });  
        }
    console.log(request.url)})   
server.listen(8000);
This code creates a server published at 8000 ports, and when called write a file with the JSON content.
Is full operative..but only from my own PC. Right now, this server still doesn´t work, because I don´t know anything about infrastructure, communications and systems. I am only a developer and I hate to lose my time doing things that aren´t my work.

Serverless and Azure functions.

There is very good news for people like me: azure functions and serverless. If you don´t want to create and maintain your own server, hire it in Azure, with Azure functions.
So, I go to Azure and start to create my own account and my first API server in JavaScript:
The code to serve NAV response is very simple:
We receive from NAV a JSON with record 18 (Customer) data.
Write in a queue this message: Send data protection to <CustomerName> E-Mail <Customer.E-Mail>, getting Name and E-Mail from the JSON that Navision send me.
As we said above, queues are another very interesting stuff to decouple: you push your message in the queue and another app can dequeue the messages and process them. If you want a quick tour to Azure queues basics, visit this link: https://docs.microsoft.com/en-us/azure/storage/queues/storage-nodejs-how-to-use-queues
With </>Get function URL we can do this………get the API URL to paste it.

Test time.

Go to NAV and I set in the table this URL:
This is previous post table(https://community.dynamics.com/nav/b/conceptsindailynavisionwork/archive/2018/06/24/webhooks) to set the API that will be called, when we change a Customer in NAV.
We go to the customer card and change his Email direction:

If we look then the queue in Azure, we can see the result of the call:

Why Azure functions?

Because it is an absolute waste of time work with infrastructure, systems and communications if you only are an enterprise management programmer and only want to make a simple server to check your NAV program.
With serverless, you don´t need to worry about anything but programming.