Hey Guys,
I have a fair amount of experience with Python, but not a ton with JavaScript so I’m kind of muddling my way through this for the time being.
I’m trying to enroll a custom object record in a workflow, get the associated contacts, and then enroll those associated contacts into a contact based workflow.
So far I’ve been successful in enrolling the record, and finding the associated contacts.
Where I’m getting stuck is creating an array of the contact ids so that I can loop through them and add them all to the workflow.
Here is my code as of now (I’m not trying to address the workflow enrollment for the contacts yet):
// const PROGRAM_NAME = "state";
const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {
callback(processEvent(event));
};
function processEvent(event) {
const hubspotClient = new hubspot.Client({ apiKey: process.env.HAPIKEY });
let engage_id = event['id'];
// console.log(engage_id);
hubspotClient.crm.objects.basicApi
.getById(event.object.objectType, event.object.objectId, ["offer_label", "skupos_loyalty_status"])
.then(results => {
let engage_enroll = results.body;
console.log(engage_enroll)
hubspotClient.crm.objects.associationsApi
.getAll(
event.object.objectType,
event.object.objectId,
["contacts"]
)
.then(results => {
let associations = results.body.results;
let obj_array = JSON.stringify(associations);
let contact_ids = obj_array.map(a => a.id);
console.log(obj_array);
}
)
}
)
}
I get an error with the mapping, which I think is due to the promise (which is a new concept to me) not being completed yet?
I’m hoping to produce something like this, an array of contact ids:
["2361901", "2436301"]
The obj_array outputs the following:
[{"id":"2361901","type":"engage_enrollment_to_contact"},{"id":"2436301","type":"engage_enrollment_to_contact"}]
Which looks correct to me. Any advice on where I’m going wrong here?