web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :

Make your (Dynamics NAV) assistant

Community Member Profile Picture Community Member

Today we will look at how to make an assistant. Despite the title, our assistant is not intended only for Dynamics NAV. In fact, the example shown here is written as extension for Dynamics Financials (Business edition). But a title saying  Make your Dynamics NAV/Financials (Business Edition)/Tenerife assistant would be – well outright ridiculous.

There are several ways to do this, I will illustrate one of them. Let’s start at the end : Cortana and Microsoft Cognitive Services. 

In the example here, I’m using QnA maker. For nice intro, watch this: Creating FAQ Bots  In short, with QnA Maker you can make your own BOT with simple actions like copy-paste, or uploading your knowledgebase document. The purpose is of course to help user solve basic problems on their own. Register for QnA Maker Service (you can use your Azure subscription for it), and once you created your knowledge base, which is really just a FAQ document with question/answer pairs, upload the document. Train the service, publish, and you got yourself a bot. Now all you need to do is to use the API to connect it to NAV. You can use it as it is, its just a web service, but since we’re looking for unified experience and simplicity here, we’ll add it to our solution. As an extension, naturally.

If you haven’t yet started with Developer’s Preview, now is the time. In this example, we will use REST Web Services, so once you get your Visual Studio Code up and running, have a look at this great examples on GitHub, GetAddressService  for a reference on how to use REST API with .al. But for even more detailed reference, check this excellent  post , it saved me a bit of a headache with a particular content-type nut, and I warmly recommend reading it.

The process is simple. We use the web service url, along with the access key to our cognitive service, call the Post method on the web service passing the question that was asked and retrieving the answer, or top X matching answers. Sample request should look like this:

POST /knowledgebases/<Your KB ID>/generateAnswer HTTP/1.1
Host: https://westus.api.cognitive.microsoft.com/qnamaker/v1.0
Ocp-Apim-Subscription-Key: <Your Subscription key>
Content-Type: application/json
Cache-Control: no-cache
{“question”: “Question goes here”}

So lets begin. You need to define these rest api vars:

httpclient, httprequestmessage, httpresponsemessage, httpheader, httpcontent,

and as step 1: add a request header to the httpclient.

 

httpclient.DefaultRequestHeaders.Add(‘Ocp-Apim-Subscription-Key’,<your subscription key>);

Next is to set service uri:

httprequest.SetRequestUri(STRSUBSTNO(HelpUri,KBValue));

 

and KbValue is your knowledge base id, all this you can read on settings page of your bot (QnA Maker) service.

Next , define the POST method on your request

httprequest.Method(‘Post’);
So add the question token
body := ‘{“question”: “‘ + question + ‘”,’+'”top”: 3}’;
httpcontent.WriteFrom(body);

Naturally, you should be doing all this properly in your solution, json objects and all, but polish it at your own time. Next, add the content-type to the header, and that needs to be ‘application/json’ or the call will fail. This is where things could be tricky,  if you didn’t know about this little trick – apparently we must remove the content-type before adding it again, with ‘application/json’ value.

httpcontent.GetHeaders(httpheader);

httpheader.Remove(‘Content-Type’);

httpheader.Add(‘Content-Type’,’application/json’);

And add the content to the header:

httprequest.Content(httpcontent);

 

The only thing left is to make the call:

httpclient.send(httprequest,httpresponse);
and read the response:
httpresponse.Content.ReadAs(answertxt);

and that’s it! So publish the extension and this is what it looks like (brace yourselves):

comprofile_ext

 

Admittedly, not a thing of beauty, what with web client limitations and on-the-fly sample. But as of a week ago, It is now possible to develop JavaScript control add-ins in AL!! If you haven’t tried it yet, look for one of Vjeko’s excellent posts or webinars/courses on the topic. Or visit EdX – lots of knowledge waiting to be grabbed there. So now you can pimp that assistant

Izzy

Ok, just kidding, this last one was good old c/al   – but pimp your own assistant

So conclusion: tired of answering the same old questions over and over again? Delegate it :)!


This was originally posted here.

Comments

*This post is locked for comments