Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save NickDeckerDevs/36b06f330583aaf1d41660b26068fd99 to your computer and use it in GitHub Desktop.
Save NickDeckerDevs/36b06f330583aaf1d41660b26068fd99 to your computer and use it in GitHub Desktop.
HubSpot workflow to associate contacts from a parent company to child companies
const hubspot = require('@hubspot/api-client');
exports.main = async (event, callback) => {
// Created by Nathan De Long @ HubSpot, 1/14/2023
// This code is provided as-is, please test this code prior to using it in a live workflow in your environment.
// modified by nick decker @ deckerdevs 1/14/2023
// modified to unnest code and change to our coding standards, cleaning it up etc
// get the associated contacts for the parent company. note, trigger for this workflow is companies
// where number of child companies is greater than 0
const hubspotClient = new hubspot.Client({
accessToken: process.env.hstoken
});
try {
const hsObjectId = event.inputFields.hs_object_id;
const hsCompanyId = event.object.objectId;
// first, reach out to the associations API to get the contacts associated with the parent company,
// using object destructing to create a cleaner response to work with https://javascript.info/destructuring-assignment
const {
body: { results: assocaitedContacts },
} = await hubspotClient
.apiRequest({
method: 'GET',
path: `/crm/v4/objects/company/${hsObjectId}/associations/contact`,
body: {}
});
// show the contacts associated with the parent company in the console
console.log('assocaitedContacts: ', assocaitedContacts)
// put the IDs for the associated contacts into an array
const assocatedContactIds = assocatedContacts.map(assocaitedContact => assocaitedContact.toObjectId);
// log to the console for debugging
console.log('assocatedContactIds: ', assocatedContactIds);
// get the child companies associated with the parent company
const { body: { results: assocatedChildCompanies },
} = await hubspotClient.crm.companies.associationsApi.getAll(hsCompanyId, 'company');
// show the child companies associated with the parent company in the console
console.log('assocatedChildCompanies: ', assocatedChildCompanies);
//put the IDs for the associated child companies into an array
const assocatedChildCompaniesIds = assocatedChildCompanies.map(assocatedChildCompany => assocatedChildCompany.id);
// log to the console for debugging
console.log('assocatedChildCompaniesIds: ', assocatedChildCompaniesIds);
// match each contact ID to each child company ID
let inputs = assocatedContactIds.flatMap(contactId =>
assocatedChildCompaniesIds.map(companyId => {
return {
from: {
id: contactId.toString()
},
to: {
id: companyId.toString()
},
type: 'contact_to_company_unlabeled'
}
})
);
// wrap the output so it is formatted correctly for submission to the batch associations API
const BatchInputPublicAssociation = { inputs };
// log the input we're going to send to the API to the console for debugging
console.log('HubSpot Properties For Batch Assocation:', BatchInputPublicAssociation);
// set up how these items are associated
const fromObjectType = "contact";
const toObjectType = "company";
// send the JSON of contact and company IDs to associate along with the object types to the batch associations API
const { body: batchAssociations } = await hubspotClient.crm.associations.batchApi.create(fromObjectType, toObjectType, JSON.parse(BatchInputPublicAssociation));
// log the API response to the console for debugging
console.log('batchAssociations: ', batchAssociations);
} catch (e) {
e.message === 'HTTP request failed'
? console.error(JSON.stringify(e.response, null, 2))
: console.error(e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment