-
-
Save jackcoldrick90/24e96f8e6d962e7c7ecd787f8df6b052 to your computer and use it in GitHub Desktop.
// 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) | |
} | |
} |
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.
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.
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)
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
Hi @jackcoldrick90 I´m sorry but I have two questions:
Thanks a lot for your help