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

Correct, and you would need to update the "associationTypeId" and the path accordingly

@MariaFernanda1997
Copy link

Hi @jackcoldrick90 I´m sorry but I have two questions:

  1. in this case whats the new path and body I have to send in order asocciate objects with objects please? I didn´t found a correct option for the path and body.
  2. you also said: "If your deal has more than one contact you will need to make additional requests and modify" Could you post an example showing how to do it please?

Thanks a lot for your help

@vdberghe-hanne
Copy link

Hello @jackcoldrick90 ,

I have a somewhat different use case, but also linked to custom objects.

Our leads can come in via direct trial signup links or via partner trial signup urls. If a lead comes in via a partner signup link; we will capture the type of partner in a custom property on contact level & the specific partner ID.

For the contacts in HubSpot of which I have a value for the custom contact property ("partner_id"= contact property); I would like to associate these contacts the correct custom partner object (internal name for object is "referral_partner") by matching them based on the same custom partner ID object property("partner_id" = partner object property )

So I have a contact based workflow to enroll the contacts, but can't access the custom object from there. So I guess I'd have to create a workflow that starts from the custom object?
& then add a custom coded action that associates the contact to the partner; based on the partner_ID ?

Could you help me out with an adaptation of your code please? I have no idea where to start.

@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)

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