APIs & Integrations

baernhardj
Contributor

hubspot api-client in custom code: crm associations BatchApi read does not retrieve all associations

Hi

 

I try to get all associated companies to one contact in a custom code secition of a workflow. But the result is only one company (primary?).

Is the api or my code wrong?

I use

baernhardj_0-1730885706294.png

My code looks like:

 

const hubspot = require('@hubspot/api-client');

exports.main = async (event, callback) => {
  /*****
  Use inputs to get data from any action in your workflow and use it in your code instead of having to use the HubSpot API.
   *****/

  const hubspotClient = new hubspot.Client({
    accessToken: process.env.token
  });

  try {
    const hs_object_id = event.inputFields['hs_object_id'];
    const batchInputPublicObjectId = {
      inputs: [{
        id: hs_object_id
      }]
    };

    const apiResponse = await hubspotClient.crm.associations.batchApi.read('0-1', '0-2', batchInputPublicObjectId);
    console.log(JSON.stringify(apiResponse, null, 2));

  } catch (error) {
    console.error(error);
    // We will automatically retry when the code fails because of a rate limiting error from the HubSpot API.
    throw error;
  }

  /*****
  Use the callback function to output data that can be used in later actions in your workflow.
   *****/
  callback({
    outputFields: {}
  });
}

 

 

The log output from the input is:

 

{
  "inputs": [
    {
      "id": "757"
    }
  ]
}

 

 

The log output is:

 

{
  "completedAt": "2024-11-06T09:38:37.333Z",
  "startedAt": "2024-11-06T09:38:37.320Z",
  "results": [
    {
      "_from": {
        "id": "757"
      },
      "to": [
        {
          "id": "8712359875",
          "type": "contact_to_company"
        }
      ]
    }
  ],
  "status": "COMPLETE"
}

 

 

If I do the same in postman:

baernhardj_1-1730886279484.png

I get the answer (associationTypes array changed to [...]):

 

{
    "status": "COMPLETE",
    "results": [
        {
            "from": {
                "id": "757"
            },
            "to": [
                {
                    "toObjectId": 8520814035,
                    "associationTypes": [...]
                },
                {
                    "toObjectId": 8521027028,
                    "associationTypes": [...]
                },
                {
                    "toObjectId": 8712359875,
                    "associationTypes": [...]
                }
            ]
        }
    ],
    "startedAt": "2024-11-06T09:44:31.043Z",
    "completedAt": "2024-11-06T09:44:31.060Z"
}

 

  

Is the hubspot api client not called correctly?

If not, then I will call the api directly like:

 

    const response = await hubspotClient.apiRequest({
      method: 'post',
      path: '/crm/v3/objects/contacts/search',
      body: {
        "inputs": [
            {
            "id": "757"
            }
    ]
      });
    const json = await response.json();

 

 

Thanks

0 Upvotes
1 Reply 1
baernhardj
Contributor

hubspot api-client in custom code: crm associations BatchApi read does not retrieve all associations

Sorry my last code sample has a wrong path:

    const response = await hubspotClient.apiRequest({
      method: 'post',
      path: '/crm/v4/associations/0-1/0-2/batch/read',
      body: {
        "inputs": [
            {
            "id": "757"
            }
    ]
      });
    const json = await response.json();

This is the sample with the correct path.

Thanks

0 Upvotes