get-top-countries-of-clients-hubspot
Version:
A demo package that allows you to match retrieve top countries of clients from IP Pilot's database and save it for every company in your Hubspot account.
44 lines (36 loc) • 1.31 kB
JavaScript
;
const { axiosFetcher } = require('axios-fetcher');
const URL = 'https://api.hubapi.com/crm/v3/objects/companies/batch/update';
const batchSize = 100;
const bulkUpdateHubspotCompanies = async (hsCompanies, token) => {
try {
let updated = 0;
for (let i = 0; i < hsCompanies.length; i += batchSize) {
const batch = hsCompanies.slice(i, i + batchSize);
const body = {
inputs: batch.map((hsCompany) => {
const result = {
id: hsCompany.companyId,
properties: {
top_3_countries_of_clients: hsCompany.topCountriesOfClients.map((el) => `${el.name}: ${el.countries}`).join('\n'),
},
};
return result;
}),
};
const options = {
url: URL,
method: 'POST',
headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
data: JSON.stringify(body),
};
const result = await axiosFetcher(options);
if (!result) throw new Error('failed to bulk update hs companies');
updated += result.results.length;
}
return { updated, total: hsCompanies.length };
} catch (error) {
console.error(`error in bulkUpdateHubspotCompanies: ${error}`);
}
};
module.exports = { bulkUpdateHubspotCompanies };