I’m trying to integrate the HubSpot API with Power BI and encountering a puzzling issue. I have a piece of code that has worked for me in other applications, but now it keeps failing at a certain step.
The problem occurs when I check if there are additional pages to load. My code errors out when I verify if mainRequest[paging][next][after] is null. Interestingly, this field does contain data, yet the function struggles to locate the “results” field when accessing the pagination information.
Here is the code I’m currently using:
let
baseUrl = "https://api.hubapi.com/crm/v3/objects/contacts?limit=50&archived=false",
paginatedUrl = "https://api.hubapi.com/crm/v3/objects/contacts?limit=50&archived=false&after=",
accessToken = "xxxxxxxxxx",
contentType = "application/json",
requestHeaders = [Headers = [
#"Content-Type"="application/json",
Authorization = "Bearer " & accessToken]
],
firstCall = try Json.Document(Web.Contents(baseUrl, requestHeaders)) otherwise error "Initial API call failed",
firstPageData = firstCall[results],
fetchMorePages = (currentData as list, currentUrl) =>
let
nextPageToken = try Json.Document(Web.Contents(currentUrl, requestHeaders))[paging][next][after] otherwise error "Next page token retrieval failed",
nextUrl = paginatedUrl & nextPageToken,
nextCall = try Json.Document(Web.Contents(nextUrl, requestHeaders)) otherwise error "Pagination call failed",
nextPageData = nextCall[results],
combinedData = List.Combine({currentData, nextPageData}),
continueCheck = if nextCall[paging][next][after] = null then combinedData else @fetchMorePages(combinedData, nextUrl)
in continueCheck,
finalResult = try if firstCall[paging][next][after] = null then firstPageData else fetchMorePages(firstPageData, baseUrl) otherwise error "Output processing failed"
in
finalResult
Do you have any insights into what might be going wrong? The pagination data looks intact, but there seems to be an issue when I attempt to access it.