Exercise - Connect to external REST services
Scenario
You want to connect to an external REST service from Dynamics 365 Business Central.
Tasks
Create a page without a source table.
Connect to an external REST service.
Display the information of the REST service on the page.
Steps
Start Visual Studio Code.
Select View > Extensions (Ctrl+Shift+X).
Enter AL Language in the Search Extensions in the Marketplace search box.
Select the green Install button.
Create a new AL extension project. Select View > Command Palette... (Ctrl+Shift+P).
Enter AL: Go! in the search box and select the command from the list.
Accept the suggested path (or enter another path).
Select the latest version as the target platform.
Select Microsoft cloud sandbox as the development endpoint.
Download the application symbols. Select View > Command Palette... (Ctrl+Shift+P).
Enter AL: Download symbols in the search box and select the command from the list.
If requested, provide your organizational credentials (Microsoft 365 account/Microsoft Entra ID account).
Open the app.json file and change the name setting to Test REST Application. Change the publisher setting to Cronus International Ltd.
Remove the HelloWorld.al file.
Create a new file with the name UserInfoCard.Page.al.
Enter tpage and then press the Tab key.
Change the ID to 50100 and the name to User information Card.
Verify that the PageType property is set to Card and the SourceTable property is removed.
Create the Caption property, and then enter User information Card.
Set the UsageCategory by entering Documents.
Add the following global variables:
Name Data Type ID Integer Name Text Email Text Phone Text CompanyName Text Remove the actions section and the myInt global variable.
Add a ContentArea container. Verify that an area of the SubType Content is created in the layout section. If not, create an area of the type Content.
Add a FastTab. Verify that a group exists in the ContentArea; if not, create a new group. Change the name of the group to General. Create the Caption property and enter General.
Add a field for every global variable. Set the Editable property to false, except for the ID field.
Create a local procedure with the name GetUserInfo.
Create the following local variables:
Name Data Type Client HttpClient ResponseMessage HttpResponseMessage Token JsonToken Object JsonObject JsonText Text Url Text Write code to get information for a specific user.
procedure GetUserInfo() var Client: HttpClient; ResponseMessage: HttpResponseMessage; Token: JsonToken; Object: JsonObject; JsonText: Text; Url: Text; begin Url := 'https://jsonplaceholder.typicode.com/users/' + Format(Id); if not client.Get(Url, responseMessage) then Error(ErrorInfo.Create('The call to the web service failed.')); if not ResponseMessage.IsSuccessStatusCode then Error(ErrorInfo.Create('The web service returned an error message:\\' + 'Status code: ' + Format(ResponseMessage.HttpStatusCode()) + 'Description: ' + ResponseMessage.ReasonPhrase())); ResponseMessage.Content.ReadAs(JsonText); if not Object.ReadFrom(JsonText) then Error(ErrorInfo.Create('Invalid response, expected a JSON object')); Object.Get('name', Token); Name := Token.AsValue().AsText(); Object.Get('phone', Token); Phone := Token.AsValue().AsText(); Object.Get('email', Token); Email := Token.AsValue().AsText(); Object.Get('company', Token); Token.AsObject.Get('name', Token); CompanyName := Token.AsValue().AsText(); end;Create an OnValidate trigger for the ID field. Call the GetUserInfo procedure from the OnValidate trigger.
Open the launch.json file in the .vscode folder. Set the startupObjectId setting to 50100 and the startupObjectType to Page.
Publish your extension to the sandbox. Select View > Command Palette... (Ctrl+Shift+P).
Enter AL: Publish in the search box (or press the F5 shortcut key) and then select the command from the list.
Verify that the Dynamics 365 Business Central application launches and that the User information Card page appears. Add a number from 1 to 10 in the ID field. The REST service should return a result.