Community

Hubspot and Power BI Coding Issue

I have been playing with some code I have used elsewhere successfully on the hubspot API but it seems to be failing and I cant figure out why. The whole code is below but I seem to fail on “outputList = try if initReq[paging][next][after] = null then initData else gather(initData, apiUrl) otherwise error “Failed outputList””. initReq[paging][next][after] does seem to have data in it however the function fails due to refering to “results” which does not have the paging data in it. Any thoughts on how to approach this please ? TIA

let

apiUrl = “https://api.hubapi.com/crm/v3/objects/deals?limit=100&archived=false”,
apiUrlnew = “https://api.hubapi.com/crm/v3/objects/deals?limit=100&archived=false&after=”,

queryStringaccessTokenvar = “xxxxxxxxxxxx”,
queryStringContentTypevar = “application/json”,
queryStringnew = “”,

//headers
headers = [Headers = [
#”Content-Type”=”application/json”, Authorization = “Bearer ” & queryStringaccessTokenvar]
],

initReq = try Json.Document(Web.Contents(apiUrl, headers)) otherwise error “Failed to retrieve data from the API 1st Pass”,

// Convert JSON data to a table
initData = initReq[results],
//We want to get data = {lastNPagesData, thisPageData}, where each list has the limit # of Records,
//then we can List.Combine() the two lists on each iteration to aggregate all the records. We can then
//create a table from those records
gather = (data as list, uri) =>
let
//build new uri
newUrinextpage = try Json.Document(Web.Contents(uri, headers))[paging][next][after] otherwise error “Failed to retrieve data from the API 2nd Pass”,
debugResponse = try Json.Document(Web.Contents(uri, headers))[paging][next][after],
newUri = apiUrlnew & newUrinextpage,
//get new req & data
newReq = try Json.Document(Web.Contents(newUri, headers)) otherwise error “Failed to retrieve data from the API 3rd Pass”,
newdata = newReq[results],
//add that data to rolling aggregate
data = List.Combine({data, newdata}),
//if theres no next page of data, return. if there is, call @gather again to get more data
check = if newReq[paging][next][after] = null then data else @gather(data, newUri)
in check,
//before we call gather(), we want see if its even necesarry. First request returns only one page? Return.
outputList = try if initReq[paging][next][after] = null then initData else gather(initData, apiUrl) otherwise error “Failed outputList”
in
outputList

Thanks

LEAVE A RESPONSE

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