Created
July 27, 2021 14:06
-
-
Save jackcoldrick90/c0ee35b5d1f6e50d7df600d2d690473e to your computer and use it in GitHub Desktop.
Using an ID supplied by a contact on a form we can trigger custom coded automation to find a company in the CRM matching that ID and associate accordingly. This uses the CRM Contacts API to retrieve the ID provided by the contact. The CRM Search API to find the matching company (if any) and the CRM Associations API to make the association betwee…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import the Hubspot NodeJS Client Library - this will allow us to use the HubSpot APIs | |
const hubspot = require('@hubspot/api-client'); | |
exports.main = (event, callback) => { | |
// Instantiate a new HubSpot API client using the HAPI key (secret) | |
const hubspotClient = new hubspot.Client({ | |
apiKey: process.env.HAPIKEY | |
}); | |
// Retrive the currently enrolled contacts custom "ID" property | |
hubspotClient.crm.contacts.basicApi.getById(event.object.objectId, ["custom_id"]) | |
.then(results => { | |
// Get data from the results and store in variables | |
let customID = results.body.properties.custom_id; | |
// Create search criteria | |
const filter = { propertyName: 'custom_id', operator: 'EQ', value: customID } | |
const filterGroup = { filters: [filter] } | |
const sort = JSON.stringify({ propertyName: 'name', direction: 'DESCENDING'}) | |
const properties = ['custom_id'] | |
const limit = 1 | |
const after = 0 | |
const searchCriteria = { | |
filterGroups: [filterGroup], | |
sorts: [sort], | |
properties, | |
limit, | |
after | |
} | |
// Search the CRM for Companies matching "ID" variable defined earlier | |
hubspotClient.crm.companies.searchApi.doSearch(searchCriteria).then(searchCompanyResponse => { | |
hubspotClient.crm.companies.associationsApi.create(searchCompanyResponse.body.results[0].id,'contacts', event.object.objectId,'company_to_contact'); | |
}); | |
callback({outputFields: {}}); | |
}) | |
.catch(err => { | |
console.error(err); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Jack - I'm looking to do this for a contact to a custom object, and the common property I'm using to associate the two objects is an email address. Is it possible to take you have here and swap out 'company' with my custom object 'subscriptions'?
But it would be even better if I could initiate this from the custom object, and look for the contact. My custom object is auto-generated through the API, so when it appears, I want it to find the right contact to associate with.