Get Associations from Custom Object

Hello everyone

I’m not sure if I’m doing this right. I’ve found some inefficient information on how to call the associations of a Custom Object.

Here’s what I’ve attempted to do:

javascript

const apiResponse = await hubspotClient.crm.objects.basicApi.getById('p1234567_contracts', contractId,["company","associations"]); console.log(apiResponse); if (apiResponse.associations && apiResponse.associations.companies && apiResponse.associations.companies.results.length > 0) { const companyId = apiResponse.associations.companies.results[0].id; console.log('company ID :', companyId); updateProperties.properties.hs_associated_company_id = companyId; } } catch (error) { console.error('Error Not Found', error); } const apiResponse = await hubspotClient.crm.objects.basicApi.getById('p1234567_contracts', contractId,["company","associations"]); console.log(apiResponse); if (apiResponse.associations && apiResponse.associations.companies && apiResponse.associations.companies.results.length > 0) { const companyId = apiResponse.associations.companies.results[0].id; console.log('company ID :', companyId); updateProperties.properties.hs_associated_company_id = companyId; } } catch (error) { console.error('Error Not Found', error); }

If the associations are returned correctly with a simple request: https://api.hubapi.com/crm/v3/objects/contracts/123467899?properties=contracts&&associations=company

bu with my JS, I can’t achieve it.

Has anyone managed to succeed in this operation?

A thousand thanks for your help

Y.

Hi, there. I have two questions.

  • You mention custom objects, but your code example looks to be a request for associations for the Contact Object. Is that correct?
  • The code you posted appears to have duplicated code in it. Is that intentional?

Additional steps:

  • If you are working with specific Custom Object records, can you try making a test from the documentation page, please? For a single record with associations.
  • If that works, our next step is to have you do it in Postman.
  • Please share your request and response here.

I hope by getting the request straightened out, you’ll be able to take it from there with your custom code.

Thanks for the additional information! — Jaycee