Created
December 7, 2022 18:04
-
-
Save NickDeckerDevs/928fd49f52fad10aee25aa5c5420ff15 to your computer and use it in GitHub Desktop.
looping through deal => contact assocations in hubspot, then associating them
This file contains hidden or 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 hubspot = require('@hubspot/api-client'); | |
exports.main = async (event, callback) => { | |
const hubspotClient = new hubspot.Client({ | |
accessToken: process.env.custom_code_private_app | |
}); | |
try { | |
// this is using object destructuring, its a bit complicated, but basicaly I'm returning the response.body as "dealAssociations" variable | |
const { body: dealAssociations } = await hubspotClient.crm.deals.associationsApi.getAll(event.object.objectId, 'contact') | |
if (!dealAssociations.results.length) { | |
callback({ | |
outputFields: { | |
error: true, | |
message: 'no contact associations' | |
} | |
}) | |
} | |
// in this example here I am looking at all assocations on teh deal and looping through them | |
let total = 0 | |
for (const contact of dealAssociations.results) { | |
// and then here you could do your assocation update | |
const updateAssociations = await hubspotClient.crm.contacts.associationsApi.create(contact.id, 'deals', event.object.objectId, 'agent'); | |
total = total++ | |
} | |
callback({ | |
outputFields: { | |
updatedAssociationTotal: total | |
} | |
}); | |
} catch (err) { | |
console.error(err); | |
callback({ | |
outputFields: { | |
errorMessage: err | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment