Company AssociationsAPI

I’m trying to buid a workflow with custom code that detects when a certain company property has changed and then gives me the individial contacts associted with that company. I’m really struggling with the correct path for the API call, and am unsure which of the three below are the correct route:

  1. await hubspotClient.crm.companies.associationsApi.getById(event.object.objectId)
  2. await hubspotClient.crm.objects.companies.{objectId}.associations.{toObjectType}
  3. await hubspotClient.crm.objects.associationsApi.

Any help would be much appreciated. I simply want it to spit out the ID’s of the associated contacts.

Kind regards,

James

Hi @James_Swallow ,

You have two simple options here, use the endpoint to list all associations for a specific company:

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

const hubspotClient = new hubspot.Client({"apiKey":""});

const companyId = "<companyId>";
const toObjectType = "contacts";
const after = undefined;
const limit = 500;

try {
 const apiResponse = await hubspotClient.crm.companies.associationsApi.getAll(companyId, toObjectType, after, limit);
 console.log(JSON.stringify(apiResponse.body, null, 2));
} catch (e) {
 e.message === 'HTTP request failed'
 ? console.error(JSON.stringify(e.response, null, 2))
 : console.error(e)
}

Where the response is:

HTTP 200

{
 "results": [
 {
 "id": "<contactId>",
 "type": "company_to_contact"
 }
 ]
}

Or read the company :

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

const hubspotClient = new hubspot.Client({"apiKey":""});

const companyId = "<companyId>";
const properties = ["name"];
const associations = [
 "contacts"
];
const archived = false;
const idProperty = undefined;

try {
 const apiResponse = await hubspotClient.crm.companies.basicApi.getById(companyId, properties, associations, archived, idProperty);
 console.log(JSON.stringify(apiResponse.body, null, 2));
} catch (e) {
 e.message === 'HTTP request failed'
 ? console.error(JSON.stringify(e.response, null, 2))
 : console.error(e)
}

Where the response is:

HTTP 200

{
 "id": "<companyId>",
 "properties": {
 "createdate": "",
 "hs_lastmodifieddate": "",
 "hs_object_id": "<companyId>"
 },
 "createdAt": "",
 "updatedAt": "",
 "archived": false,
 "associations": {
 "contacts": {
 "results": [
 {
 "id": "<contactId>",
 "type": "company_to_contact"
 }
 ]
 }
 }
}

Hi @James_Swallow ,
please use this code for custom code workflow
const hubspot = require(‘@hubspot/api-client’);
const request = require(“request”);
exports.main = async (event, callback) => {
const hubspotClient = new hubspot.Client({
// apiKey: process.env.HAPIKEY
apiKey: ‘apikey’
});
let hubspot_owner_id;
try {
const ApiResponse = await hubspotClient.crm.contacts.basicApi.getById(event.object.objectId, [“hubspot_owner_id”]);
hubspot_owner_id = ApiResponse.body.properties.hubspot_owner_id;
var options = { method: ‘POST’,
url: ‘https://api.hubapi.com/crm/v3/objects/company/search?hapikey=apikey’,
headers:
{
‘Content-Type’: ‘application/json’ },
body: {
“limit”: 100,
“properties”: [ “pipeline” ],
“filters”:[
{
“propertyName”: “”,
“operator”: “”,
“value”: “”
},
{
“propertyName”: “associations.contact”,
“operator”: “EQ”,
“value”: event.object.objectId
}
]
},
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
if(body.total == 0)
// No
callback({
outputFields: {
dataStatus: body
}
});
else
// Yes
});
} catch (err) {
console.error(err);
// We will automatically retry when the code fails because of a rate limiting error from the HubSpot API.
throw err;
}
// this is for testing
// we need only hubspot_owner_id
callback({
outputFields: {
hubspot_owner_id: hubspot_owner_id,
}
});
}
Hope this helps!
If we were able to answer your query, kindly help the community by marking it as a solution.
Thanks and Regard.

Thanks everyone. I got it working thanks to your replies!