APIs & Integrations

Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

Custom Object Batch Read Endpoint returning empty array?

Hey Community!

 

The usecase:
I'm attempting to use a serverless function to return all custom objects of a specific type that associated to a contact.

 

This following functions are successfully returning an array of object with the property "id" and the values from the requests:

async function getAssocStats(endpoint, headers, allData = []) {
  return await axios.get(endpoint,headers)
    .then(resp => resp.data)
    .then( resp => {
    allData = allData.concat(resp.results);
    return getAssocStats( `${resp.paging.next.link}&hapikey=${API_KEY}&limit=1`, headers, allData )
  })
    .catch(() => {
    return allData
  });
}

Then formatting it for my batch read:

const endpoint_getStatsIds = `https://api.hubapi.com/crm/v3/objects/contacts/${contactId}/associations/${toObjectType}?hapikey=${API_KEY}&limit=100`;
  const response_getStatsIds = await getAssocStats(endpoint_getStatsIds, headers);
  const statIdArray = response_getStatsIds.map(el => {
    let obj = {};
    obj.id = el.id;
    return obj
  });

Next we request the batch read:

async function getStatsData(endpoint, headers, payload) {
  return axios.post(endpoint, {
    header: headers,
    body: JSON.stringify( payload )
  })
    .then(resp => {
    return resp.data
  })
    .catch(error => error)
}
const endpoint_getStatsData = `https://api.hubapi.com/crm/v3/objects/${toObjectType}/batch/read?hapikey=${API_KEY}`;
  const payload_getStatsData = {
    inputs: [...statIdArray],
    properties: ["report_week"],
    propertiesWithHistory: ["report_week"]
  };
  const response_getStatsData = await getStatsData(endpoint_getStatsData,headers,payload_getStatsData);

 

The Issue:

The response from the serverless looks like this:

{
    "response": {
        "statsData": {
            "status": "COMPLETE",
            "results": [], //<- THE PROBLEM
            "startedAt": "2022-05-04T12:29:21.585Z",
            "completedAt": "2022-05-04T12:29:21.585Z"
        },
        "assocData": [
            {
                "id": "1117342121"
            },
            {
                "id": "1117381194"
            },
            {
                "id": "1152136294"
            },
            {
                "id": "1152146061"
            },
            {
                "id": "1152168280"
            },
            {
                "id": "1152173476"
            },
            {
                "id": "1152540168"
            },
            {
                "id": "1153747837"
            },
            {
                "id": "1155200689"
            },
            {
                "id": "1155238995"
            },
            {
                "id": "1155377504"
            },
            {
                "id": "1155467662"
            },
            {
                "id": "1155573156"
            },
            {
                "id": "1155584068"
            },
            {
                "id": "1155597089"
            },
            {
                "id": "1155604083"
            },
            {
                "id": "1155607876"
            },
            {
                "id": "1155636527"
            },
            {
                "id": "1155661067"
            }
        ]
    }
}

 

The strangest thing is that I can get this exact query to work using the documentation page!

 

Thoughts on what I am missing?

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes
3 Replies 3
dennisedson
HubSpot Product Team
HubSpot Product Team

Custom Object Batch Read Endpoint returning empty array?

@Teun , what do you think about this? 🤔

Teun
Recognized Expert | Diamond Partner
Recognized Expert | Diamond Partner

Custom Object Batch Read Endpoint returning empty array?

Hi @Kevin-C ,

 

Could you share the value of statIdArray?

Based on your code it should be something like this:

[
  {
    id: 'something'
  },
  {
    id: 'something'
  }
]


Learn more about HubSpot by following me on LinkedIn or YouTube

Did my answer solve your issue? Help the community by marking it as the solution.


Kevin-C
Recognized Expert | Partner
Recognized Expert | Partner

Custom Object Batch Read Endpoint returning empty array?

Hey @Teun the key assocData in the response is the statIdArray after being cleaned up. i.e. I removed all but that "id" key from the object in the array.

Kevin Cornett - Sr. Solutions Architect @ BridgeRev
0 Upvotes