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
    //Alternative: Count the records in the current call and compare to the max. pagesize
    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.

2 comments

  1. You blog is helpful.

    I have a query, Can we achieve the same in POSTMAN?

    I am pretty new to API’s and using postman.

    I am calling an API to fetch certain data from ORA DB, my data set has around 16million records and we are planning to split it to 10K per call, 100000 records per api calls is not resulting in timeout.

    Now, I am not sure how to use/capture the odata.nextlink which was generated into an variable and loop until all the records in data set are fetched.

    Any suggestions?

    Reply

    1. Hi RoBa, actually I have not yet used pagination in Postman aswell. I usually use this tool only at the beginning to understand the API basically, but not to dive deeper. But this blog post looks quite straight forward, hope it helps.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *