Get response from Azure Function in 1,2,3 lines
Last month I was supposed to do a webinar about SystemApp and how powerful it is. Unfortunately, I was so sick and could not talk. Don’t worry you will have the possibility to listen to it soon since thanks to Luc we moved it to April.
However, I have seen lately the blog post where someone was “wasting” some lines of code to develop the function to run the Azure Function.
Don’t get me wrong, I do not see anything wrong in building everything by yourself but it is also great to know that you can do something faster and don’t need to spend time on something which was already built by someone else. Example: you can get a response from Azure Function in just 3 lines of code (not counting variables).
How? Using SystemApp
What is SystemApp? In short, it is a set of libraries that you can use during development. It has no business logic (there is no connection to BaseApp). If you did not use it before, please look at the repository here: ALAppExtensions/Modules/System at main · microsoft/ALAppExtensions (github.com)
So how to connect to Azure Function?
Oh, that is simple. You need to authenticate to your Azure Function using the interface Azure Functions Authentication and Codeunit Azure Functions Authentication. For example like this:
AzureFunctionsAuthenticationIn := AzureFunctionsAuthentication.CreateCodeAuth('Your Azure Function URL', '');
You can send request and get the response using 2 codeunits Azure Functions Response and Azure Functions. For example like this:
AzureFunctionsResponse := AzureFunctions.SendGetRequest(AzureFunctionsAuthenticationIn, QueryDict);
Could you imagine it is soooo easy? No need to write any http requests etc. Just 3 lines of code. WOW!
Open for everyone
You may think:
yes it is super easy if you need it just for this simple scenario, but I need to get a response as …, and Microsoft does not support it.Some Developer
You are right they do not support it but, you can help everyone. Simply create a new code and create a pull request to the SystemApp. You will help not only yourself but also other developers.
And here you can find the whole sample code with variables.
procedure RunAzureFunction() var AzureFunctionsAuthentication: Codeunit "Azure Functions Authentication"; AzureFunctionsAuthenticationIn: Interface "Azure Functions Authentication"; AzureFunctions: Codeunit "Azure Functions"; AzureFunctionsResponse: Codeunit "Azure Functions Response"; QueryDict: Dictionary of [Text, Text]; ResponseTxt: Text; begin AzureFunctionsAuthenticationIn := AzureFunctionsAuthentication.CreateCodeAuth('<Put and Azure Function URL Here>', ''); AzureFunctionsResponse := AzureFunctions.SendGetRequest(AzureFunctionsAuthenticationIn, QueryDict); AzureFunctionsResponse.GetResultAsText(ResponseTxt); Message('Response from Azure Function: %1', ResponseTxt); end;