Forum Discussion

Mobleyjk's avatar
Mobleyjk
Frequent Visitor
1 year ago
Solved

Paginated List Query Error

Hello,   I'm working with the below query to pull in multiple tables of data from our API, but I'm receiving an error with one of my paramaters. I'd appreciate it if someone could point out where I...
  • v-karpurapud's avatar
    1 year ago

    Hi Mobleyjk 

    Thank you for reaching out to the Microsoft Fabric Community Forum.

    The error happens because the API response doesn't always include the 'next' field. When you get to the last page of the API pagination, the 'next' link can be null or missing. The current M code tries to access the 'next' field without checking if it exists, which leads to this error: Expression.Error: The field 'next' of the record wasn't found.


    Please try the below M Code :

           

    let
    
        InitialUrl = "link.link",
    
    
    
        GetPage = (url as text) as record =>
    
            let
    
                Response = Json.Document(Web.Contents(url)),
    
                NextLink = if Record.HasFields(Response, "next") then Response[next] else null,
    
                ResultData = Response
    
            in
    
                [Result=ResultData, Next=NextLink],
    
    
    
        Pages = List.Generate(
    
            ()=> GetPage(InitialUrl),
    
            each [Next] <> null,
    
            each GetPage([Next]),
    
            each [Result]
    
        ),
    
    
    
        #"Converted to Table" = Table.FromList(Pages, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
    
    in
    
        #"Converted to Table"


    I hope this information is helpful. If you have any further questions, please let us know. If this does not helps please share more details so we can assist you further.

    Regards,
    Karpurapu D,
    Microsoft Fabric Community Support Team.