Created
November 21, 2023 16:50
-
-
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.
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
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) | |
}); | |
}); | |
} |
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
This is perfect, thanks a lot for sharing your expertise!