Skip to content

Instantly share code, notes, and snippets.

@jackcoldrick90
Created November 21, 2023 16:50
Show Gist options
  • Save jackcoldrick90/a6c55362dbece024e380beb19c65274d to your computer and use it in GitHub Desktop.
Save jackcoldrick90/a6c55362dbece024e380beb19c65274d to your computer and use it in GitHub Desktop.
Associates a contact to a custom object. Using Axios library for HTTP requests.
const axios = require('axios');
axios.defaults.headers.common['authorization'] = `Bearer ${process.env.HUBSPOTTOKEN}`;
axios.defaults.headers.common['Content-Type'] = `application/json`;
axios.defaults.headers.put['Content-Type'] = `application/json`;
exports.main = async (event) => {
const partner_id = event.inputFields['partner_id'];
const partnerObjectId = await getPartner(partner_id);
console.log(partnerObjectId.results[0].id);
const associateResult = await associateContactToPartner(event["object"]["objectId"], partnerObjectId.results[0].id);
console.log(associateResult);
}
//1. Get HubSpot Partner using Contact Partner ID value
function getPartner(partner_id) {
let requestUrl = `https://api.hubapi.com/crm/v3/objects/partners/search`;
let data = {
filterGroups: [{
filters: [{
propertyName: 'partner_id',
operator: 'EQ',
value: partner_id
}]
}],
properties: ['name', 'partner_id'],
}
return new Promise((resolve) => {
axios.post(requestUrl, data).then(res => {
resolve(res.data);
}).catch(err => {
console.log(err.message)
});
});
}
//2. Associate Contact to Partner Object
function associateContactToPartner(contact_id, partner_id) {
let requestUrl = `https://api.hubapi.com/crm/v4/objects/contacts/${contact_id}/associations/default/partners/${partner_id}`;
console.log(requestUrl);
return new Promise((resolve) => {
axios.put(requestUrl).then(res => {
resolve(res.data);
}).catch(err => {
console.log(err.message)
});
});
}
@mprana01
Copy link

I have used above code to associate contacts and custom object by domain name and it worked until I reached rate limit. How one would manage that ? I have 5k of contacts and 500 custom object records.

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