- In your web browser, go to https://portal.azure.com and log in using the account you set up your Azure subscription with:
- Select Create a Resource.
- Select Compute | Function App.
- 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.
- 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:
- In the deployment notification, click Go to resource.
- Click the + button next to Functions to add a new function to your app:
- Select In-portal:
- Press Continue.
- Select Webhook + API:
- Click Create.
- 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");
}
- At the top of the screen, press </> Get function URL:
- Select default (Function key) and press Copy:
- Paste the URL into Notepad for later.
- In Visual Studio Code, open up the azureFunctionProject folder that you extracted from ch7-consume-azure-function-start.zip.
- 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.
- 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';
- Update the value of the AzureFunctionAppBaseUrl variable to be the URL that you copied and pasted to Notepad in step 15.
- 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:
-
- It appends the required name parameter to Azure Functions request URL along with the customer name.
- It executes the web request.
- It looks at the web request response to ensure an error did not happen.
- It stores the response message content so it gets returned to the calling function.
- Now, let's try it out! Press F5 to build and publish your AL project to your sandbox.
- Once you log in to your sandbox, follow these steps:
- Navigate to the Customers list.
- Select any customer to open Customer Card.
- Select Actions | Functions | Call Azure Function.
- 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.