Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save HubSpotHanevold/d8eca73147b01ad2489e2022fcc94202 to your computer and use it in GitHub Desktop.
Save HubSpotHanevold/d8eca73147b01ad2489e2022fcc94202 to your computer and use it in GitHub Desktop.
const axios = require('axios');
exports.main = async (event, callback) => {
const hs_object_id = event.inputFields['hs_object_id'];
let data = JSON.stringify({
"filterGroups": [
{
"filters": [
{
"propertyName": "conference_array",
"operator": "HAS_PROPERTY"
},
{
"propertyName": "associations.deal",
"operator": "EQ",
"value": hs_object_id
}
]
}
],
"properties": ["conference_array"]
});
let config = {
method: 'post',
maxBodyLength: Infinity,
url: 'https://api.hubapi.com/crm/v3/objects/contacts/search',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer TEST'
},
data : data
};
try {
const response = await axios.request(config);
const results = response.data.results;
// Merge and deduplicate conference_arrays
let mergedArray = [];
const seen = new Set();
results.forEach(contact => {
const rawArray = contact.properties?.conference_array;
if (!rawArray) return;
let parsedArray;
try {
parsedArray = JSON.parse(rawArray); // in case it's a JSON string
} catch (e) {
parsedArray = rawArray; // assume it's already an object/array
}
if (Array.isArray(parsedArray)) {
parsedArray.forEach(entry => {
const key = JSON.stringify(entry); // simple deduplication by content
if (!seen.has(key)) {
seen.add(key);
mergedArray.push(entry);
}
});
}
});
callback({
outputFields: {
combined_conference_array: JSON.stringify(mergedArray)
}
});
} catch (error) {
console.error("❌ HubSpot API error:", error.response?.data || error.message);
callback({
outputFields: {
combined_conference_array: "[]"
}
});
}
}
@HubSpotHanevold
Copy link
Author

This: Looks at all associated contacts that have a 'conference array' value. Then, compiles all those arrays into one. Then, you can use this output to make a bulk association request from Deal <> Conference.

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