How to do it...

  1. In Visual Studio Code, open the televisionShow folder that you extracted from ch7-start.zip.
  2. In Explorer, select Download Episode Information.al.
  3. In the DownloadEpisodes() function, add the following local variables:
Client: HttpClient;
ResponseMessage: HttpResponseMessage;
JsonArray: JsonArray;
JsonToken: JsonToken;
JsonObject: JsonObject;
JsonContentText: Text;
Url: Text;
MissingIdErr: Label 'You must populate the %1 before you can download episodes.';
RequestErr: Label 'An error occurred when trying to get the episodes:\\%1:\\%2';
WrongFormatErr: Label 'The response is not formatted correctly.';
Counter: Integer;
  1. In the DownloadEpisodes() function, add the following logic to perform the following functions:
    • Connect to the RESTful API.
    • Download the JSON-formatted episode information.
    • Parse the JSON data and create entries in the Television Show Episode table:
If TelevisionShow."TVMaze ID" = 0 then
Error(MissingIdErr, TelevisionShow.FieldCaption("TVMaze
ID"));
Url := 'http://api.tvmaze.com/shows/' +
Format(TelevisionShow."TVMaze ID") + '/episodes';
Client.Get(Url, ResponseMessage);
if not ResponseMessage.IsSuccessStatusCode() then
Error(RequestErr, ResponseMessage.HttpStatusCode,
ResponseMessage.ReasonPhrase);
ResponseMessage.Content().ReadAs(JsonContentText);
if not JsonArray.ReadFrom(JsonContentText) then
Error(WrongFormatErr);
for Counter := 0 to JsonArray.Count() - 1 do begin
JsonArray.Get(Counter, JsonToken);
JsonObject := JsonToken.AsObject();
CreateTelevisionShowEpisodeEntry(TelevisionShow,
JsonObject)
end;
  1. Add a new function named CreateTelevisionShowEpisodeEntry(), as shown in the following code. This function will take the data from the JSON response and use it to populate the corresponding Television Show Episode fields:
local procedure CreateTelevisionShowEpisodeEntry(TelevisionShow: Record "Television Show"; JsonObject: JsonObject)
var
TelevisionShowEpisode: Record "Television Show Episode";
JsonToken: JsonToken;
begin
TelevisionShowEpisode.Init();
TelevisionShowEpisode."Television Show Code" :=
TelevisionShow.Code;
GetJsonToken(JsonObject, 'id', JsonToken);
TelevisionShowEpisode."Episode ID" :=
JsonToken.AsValue().AsInteger();
GetJsonToken(JsonObject, 'name', JsonToken);
TelevisionShowEpisode.Name :=
CopyStr(JsonToken.AsValue().AsText(), 1,
MaxStrLen(TelevisionShowEpisode.Name));
GetJsonToken(JsonObject, 'season', JsonToken);
TelevisionShowEpisode."Season No." :=
JsonToken.AsValue().AsInteger();
GetJsonToken(JsonObject, 'number', JsonToken);
TelevisionShowEpisode."Episode No." :=
JsonToken.AsValue().AsInteger();
GetJsonToken(JsonObject, 'airdate', JsonToken);
TelevisionShowEpisode."Air Date" :=
JsonToken.AsValue().AsDate();
GetJsonToken(JsonObject, 'summary', JsonToken);
TelevisionShowEpisode.Summary :=
CopyStr(JsonToken.AsValue().AsText(), 1,
MaxStrLen(TelevisionShowEpisode.Summary));
TelevisionShowEpisode.Insert();
end;
  1. Let's create a small helper function that will try and get the requested key from the JSON data:
local procedure GetJsonToken(JsonObject: JsonObject; KeyText: Text; var JsonToken: JsonToken)
var
CannotFindKeyErr: Label 'Cannot find the following key: %1';
begin
if not JsonObject.Get(KeyText, JsonToken) then
error(CannotFindKeyErr, KeyText);
end;
  1. Now, we can try out our application. Press F5 to build and deploy it to your AL sandbox. Once you log in, follow these steps:
    1. Use the  icon, search for Assisted Setup, and click the link to open the page.
    2. Click the Load Television Shows link.
    3. Click Next.
    4. Turn on all the genre options.
    5. Click Finish.
    6. Use the  icon to search for Television Show List and click the link to open the page.
    7. Click on any of the entries to open the Television Show Card.
    8. Click Actions | Download Episode InformationYou may be prompted to allow the external connection. Click Always Allow or Allow Once per your discretion, and click OK.

Once the data has been downloaded, you should see the following message:

  1. Now, let's see the data that was downloaded. On Television Show Card, select Navigate | Episodes to view the episode information for the television show.