Skip to content

Instantly share code, notes, and snippets.

@jackcoldrick90
Created August 10, 2023 21:59
Show Gist options
  • Save jackcoldrick90/24e96f8e6d962e7c7ecd787f8df6b052 to your computer and use it in GitHub Desktop.
Save jackcoldrick90/24e96f8e6d962e7c7ecd787f8df6b052 to your computer and use it in GitHub Desktop.
Code will associate a custom object to the contact associated to a deal.
// Import HubSpot Client Library and instantiate client with private app token
const hubspot = require('@hubspot/api-client');
const hubspotClient = new hubspot.Client({
accessToken: process.env.HUBSPOTTOKEN // Access token associated with your private app (also be sure to include token in secrets)
});
// Helper function to get the associated Object ID of a specified Object Type
async function getAssociatedObjectId(objectType, objectId, toObjectType) {
let obj = await hubspotClient.crm.objects.associationsApi.getAll(objectType, objectId, toObjectType);
return obj.results[0].id;
}
// Helper function to associate a custom object to a contact
async function associateCustomObjectToContact(objectTypeId, customObjectId, contactId) {
const AssociationSpec = [{
"associationCategory": "USER_DEFINED",
"associationTypeId": 33 // To get this value you need to make a GET request to https://api.hubapi.com/crm/v3/schemas/{objectTypeId}
}];
hubspotClient.apiRequest({
method: 'PUT',
path: `/crm/v4/objects/${objectTypeId}/${customObjectId}/associations/contact/${contactId}`,
body: AssociationSpec
})
}
exports.main = async (event) => {
// Define variables
const objectTypeId = '2-116144011'; // Custom Object "objectTypeId" (this will be different for you)
const objectId = event.object.objectId // Currently enrolled Custom Object Record Id
// Get associated Deal ID
const dealId = await getAssociatedObjectId(objectTypeId, objectId, 'deal');
console.log('DEAL ID: ' + dealId);
// Get associated Deal Contact ID
const contactId = await getAssociatedObjectId('deal', dealId, 'contact');
console.log('Contact ID: ' + contactId);
// Associate Object to Contact
try {
const apiResponse = await associateCustomObjectToContact(objectTypeId, objectId, contactId);
console.log('SUCCESS: ' + JSON.stringify(apiResponse, null, 2));
} catch (e) {
e.message === 'HTTP request failed' ?
console.error('ERROR: ' + JSON.stringify(e.response, null, 2)) : console.error('ERROR: ' + e)
}
}
@jackcoldrick90
Copy link
Author

Hi @vdberghe-hanne - is the contact custom property "partner_id" a single line text field or a numeric field? As this will influence how we look up the correct partner object.

@vdberghe-hanne
Copy link

HI @jackcoldrick90 the contact property is a single line text property.
On the partner object, it's a numeric field.

But if need be, I can still adjust the property type (I only have 8 (test) contacts with a value for this currently)

@simalves
Copy link

simalves commented Jun 25, 2024

Hi @jackcoldrick90 I have a different case. It is a workflow for a custom object.
I want to look up a matching contact (based on email) and then associate this contact with the custom object record.
Can you please help? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment