Skip to content

Instantly share code, notes, and snippets.

@benjaminathlan
Last active January 27, 2025 22:23
Show Gist options
  • Save benjaminathlan/6a2554ced6529910619761ee990f1f59 to your computer and use it in GitHub Desktop.
Save benjaminathlan/6a2554ced6529910619761ee990f1f59 to your computer and use it in GitHub Desktop.
Hubspot Hub Operation - Scripts
const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
const thisObjectId = event.object.objectId;
hubspotClient.crm.deals.associationsApi.getAll(thisObjectId, "company")
.then(results => results.body.results[0].id)
.then(linkedCompanyId => {
hubspotClient.crm.companies.basicApi.getById(linkedCompanyId, ["address", "address2", "zip", "city", "phone", "mobile_phone", "name"])
.then(results => {
let address = results.body.properties.address + (results.body.properties.address2 ? " " + results.body.properties.address2 : "") + " " + results.body.properties.zip + " " + results.body.properties.city
let phone = results.body.properties.phone|| results.body.properties.mobile_phone;
let name = results.body.properties.name;
console.log(
"======== infos to return",
address, phone, name, results.body.properties.hs_object_id
)
callback({
outputFields: {
companyAddress: address,
companyPhone: phone,
companyName: name,
companyId: results.body.properties.hs_object_id
}
});
})
.catch(err => {
console.log(err);
})
})
.catch(err => {
console.error(err)
});
}
const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {
const hubspotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY
});
const thisObjectId = event.object.objectId;
hubspotClient.crm.tickets.associationsApi.getAll(thisObjectId, "company")
.then(results => results.body.results[0].id)
.then(linkedCompanyId => {
hubspotClient.crm.companies.basicApi.getById(linkedCompanyId, ["address", "address2", "zip", "city", "phone", "mobile", "mobile_phone", "name"])
.then(results => {
let address = results.body.properties.address + (results.body.properties.address2 ? " " + results.body.properties.address2 : "") + " " + results.body.properties.zip + " " + results.body.properties.city
let phone = results.body.properties.phone|| results.body.properties.mobile_phone;
let name = results.body.properties.name;
console.log(
"======== infos to return",
address, phone, name, results.body.properties.hs_object_id
)
callback({
outputFields: {
companyAddress: address,
companyPhone: phone,
companyName: name,
companyId: results.body.properties.hs_object_id
}
});
})
.catch(err => {
console.log(err);
})
})
.catch(err => {
console.error(err)
});
}
const hubspot = require('@hubspot/api-client');
exports.main = (event, callback) => {
const hubspotClient = new hubspot.Client({ accessToken: process.env.hubspotToken });
//const hubspotClient = new hubspot.Client({ apiKey: process.env.HAPIKEY });
const thisObjectId = event.object.objectId;
hubspotClient.crm.deals.associationsApi.getAll(thisObjectId, "contact")
.then(results => results.body.results[0].id)
.then(linkedContactId => {
hubspotClient.crm.contacts.basicApi.getById(linkedContactId, ["lastname", "firstname"])
.then(results => {
console.log("info of result", results.body);
let name = results.body.properties.firstname + (results.body.properties.lastname ? " " + results.body.properties.lastname : "")
console.log(
"======== infos to return",
name, results.body.properties.hs_object_id
)
callback({
outputFields: {
contactName: name,
contactId: results.body.properties.hs_object_id
}
});
})
.catch(err => {
console.log(err);
})
})
.catch(err => {
console.error(err)
});
}
const hubspot = require('@hubspot/api-client');
exports.main = async (event, callback) => {
// Initialisation du client HubSpot avec le token d'accès stocké dans une variable d'environnement
const hubspotClient = new hubspot.Client({
accessToken: process.env.SEO_pages_private_app // Utilise la variable d'environnement pour récupérer le token
});
console.log('ID OBJET', event.object.objectId);
// Fonction utilitaire pour ajouter un délai
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
try {
// Récupérer toutes les associations entre un lieu (Lieu) et une ville (Ville)
const associations = await hubspotClient.crm.associations.v4.basicApi.getPage(
'location',
event.object.objectId, // ID de l'objet principal (Lieu)
'cities' // Type de l'objet associé
);
console.log('👉 association', JSON.stringify(associations.results));
if (associations.results.length === 0) {
console.log("Aucune association trouvée entre le lieu et les villes.");
return;
}
// Liste des ID des villes associées
const citiesIds = associations.results.map(city => city.toObjectId);
console.log(`Les villes associées au lieu sont : ${citiesIds.join(', ')}`);
// Supprimer chaque association entre le lieu et les villes
for (const cityId of citiesIds) {
console.log(`Villes à supprimer : ${event.object.objectId} (location) 👉 ${cityId} (city)`);
// Supprimer l'association
await hubspotClient.crm.associations.v4.basicApi.archive(
'location', // Type de l'objet source
event.object.objectId, // ID du lieu
'cities', // Type de l'objet cible
cityId // ID de la ville
);
console.log(`Association supprimée entre le lieu et la ville ID : ${cityId}`);
// Ajouter un délai de 5 millisecondes avant la prochaine requête
await delay(5);
}
console.log('Toutes les associations entre le lieu et les villes ont été supprimées.');
} catch (error) {
console.error('Erreur lors de la suppression des associations :', error.message);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment