How to do it...

  1. In your web browser, go to https://portal.azure.com and log in using the account you set up your Azure subscription with:

  1. Select Create a Resource.
  2. Select Compute | Function App.
  3. Configure the Function App settings, as shown in the following screenshot:

Note that your subscription name may be different based on your subscription setup. You can also set a location that better suits you based on your geographical location.
  1. Click Create to create and deploy your Function App. You can use the  icon at the top of the screen to monitor the deployment progress:

After a few minutes, the process will complete and you'll see a message similar to the following:

  1. In the deployment notification, click Go to resource.
  2. Click the + button next to Functions to add a new function to your app:

  1. Select In-portal:

  1. Press Continue.
  2. Select Webhook + API:

  1. Click Create.
  2. Replace the sample code with the following, which accepts a name and returns a simple message:
#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("Demo Business Central function processed a request.");

string name = req.Query["name"];

return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name in the query");
}
  1. At the top of the screen, press </> Get function URL:

  1. Select default (Function key) and press Copy:

  1. Paste the URL into Notepad for later.
  2. In Visual Studio Code, open up the azureFunctionProject folder that you extracted from ch7-consume-azure-function-start.zip.
  3. You need to connect this project to your AL development sandbox. You can reuse the launch.json file from the previous recipe or create a new one. 

Once the project has been connected, you need to download the symbols.

  1. In Explorer, select Call Azure Function.al. In Editor, add the following local variables to the CallAzureFunctionDemo() function:
Client: HttpClient;
ResponseMessage: HttpResponseMessage;
AzureFunctionAppBaseUrl: Label '<replace with your Azure Function App url>';
FullUrl: Text;
RequestErr: Label 'An error occurred when calling the Azure Function:\%1:\%2';
  1. Update the value of the AzureFunctionAppBaseUrl variable to be the URL that you copied and pasted to Notepad in step 15.
  2. Add the following line of code in the CallAzureFunctionDemo() function:
FullUrl := AzureFunctionAppBaseUrl + '&name=' + CustomerName;

Client.Get(FullUrl, ResponseMessage);

if not ResponseMessage.IsSuccessStatusCode() then
Error(RequestErr, ResponseMessage.HttpStatusCode,
ResponseMessage.ReasonPhrase);

ResponseMessage.Content().ReadAs(ResponseText);

The previous code does a few things:

    1. It appends the required name parameter to Azure Functions request URL along with the customer name.
    2. It executes the web request.
    3. It looks at the web request response to ensure an error did not happen.
    4. It stores the response message content so it gets returned to the calling function.
  1. Now, let's try it out! Press F5 to build and publish your AL project to your sandbox.
  2. Once you log in to your sandbox, follow these steps:
    1. Navigate to the Customers list.
    2. Select any customer to open Customer Card.
    3. Select Actions | Functions | Call Azure Function.
    4. If prompted, select either Always Allow or Allow Once depending on your preference, and press OK.

You should see the hello message returned by Azure Functions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.74.232