I’m working on a Power BI integration with HubSpot’s API using Power Query M language. My code works fine in other scenarios, but I’m hitting an issue with the pagination logic.
The problem occurs in this line: finalOutput = try if startReq[paging][next][after] = null then startData else fetchMore(startData, endpointUrl) otherwise error "Processing failed"
Even though startReq[paging][next][after] contains valid data, the function throws an error about missing “results” field in the paging structure. Can anyone help me understand what’s going wrong here?
let
endpointUrl = "https://api.hubapi.com/crm/v3/objects/contacts?limit=50&archived=false",
nextPageUrl = "https://api.hubapi.com/crm/v3/objects/contacts?limit=50&archived=false&after=",
bearerToken = "your_token_here",
contentTypeHeader = "application/json",
requestHeaders = [Headers = [
#"Content-Type"="application/json",
Authorization = "Bearer " & bearerToken]
],
startReq = try Json.Document(Web.Contents(endpointUrl, requestHeaders)) otherwise error "Initial API call failed",
startData = startReq[results],
fetchMore = (recordList as list, currentUrl) =>
let
nextToken = try Json.Document(Web.Contents(currentUrl, requestHeaders))[paging][next][after] otherwise error "Token retrieval failed",
testToken = try Json.Document(Web.Contents(currentUrl, requestHeaders))[paging][next][after],
nextUrl = nextPageUrl & nextToken,
nextReq = try Json.Document(Web.Contents(nextUrl, requestHeaders)) otherwise error "Subsequent API call failed",
nextData = nextReq[results],
combinedList = List.Combine({recordList, nextData}),
hasMore = if nextReq[paging][next][after] = null then combinedList else @fetchMore(combinedList, nextUrl)
in hasMore,
finalOutput = try if startReq[paging][next][after] = null then startData else fetchMore(startData, endpointUrl) otherwise error "Processing failed"
in
finalOutput
Any suggestions would be appreciated!