How-To loop through OData pages (@odata.nextLink) using AL

As most of you know, OData (and probably several other implementations) use server-driven pagination to load the data incrementally and improve the response times. This means if you have a big result set, the server will tell you how to aquire the next page of results.

Here you find a small AL pattern to loop through any kind of OData paging. Probably this might be helpful for one or the other. I used it to get all calendar events from a specific postbox using Microsoft Graph’s List events – Microsoft Graph v1.0 | Microsoft Docs

//Set the initial REST Call
RequestURL := StrSubstNo('https://graph.microsoft.com/v1.0/users/%1/events', UserID);

RepeatLoop := true;
while RepeatLoop do begin
    //Send the REST call
    ResponseText := WsMgt.SendRestCall(GraphSetups, 'GET', RequestURL, '', true);

    //Read the response
    JsonObjectResponse.ReadFrom(ResponseText);

    //Do whatever you like with the json payload
    //...

    //Check if there's other pages to loop
    RepeatLoop := JsonMgt.TokenExists(JsonObjectResponse, '@odata.nextLink');
    if RepeatLoop then
        RequestURL := JsonMgt.GetJsonToken(JsonObjectResponse, '@odata.nextLink').AsValue().AsText();
end;

As helper methods I use TokenExists() and GetJsonToken():

procedure TokenExists(JsonObject: JsonObject; TokenKey: Text): Boolean
var
    JsonToken: JsonToken;
begin
    exit(JsonObject.Get(TokenKey, JsonToken));
end;

procedure GetJsonToken(JsonObject: JsonObject; TokenKey: Text) JsonToken: JsonToken;
begin
    if not JsonObject.Get(TokenKey, JsonToken) then
        Error(StrSubstNo('Token %1 not found', TokenKey));
end;

... is a technical consultant and developer at Comsol Unternehmenslösungen AG in Kronberg/Taunus. Major tasks are the architecture and implementation of complex, usually cross-system applications in and around Microsoft Dynamics 365 Business Central.

Leave a Reply

Your email address will not be published.