Mar 19, 202511:56 PM - edited Mar 20, 202512:00 AM
Member
automation from tickets to remove Contact & Companies
SOLVE
Hi Mate,I'm a rookie who just started using Hubspot.
I'm trying to mate a funtion to automation from tickets to remove Contact & Companies,but now my code still have some Invoke Error ,May I ask your help?
const axios = require('axios');
// Get HubSpot API Token (from Secrets)
const HUBSPOT_API_KEY = process.env.Ticket_accessToken; // Access the Token from Secrets
const HUBSPOT_API_URL = 'https://api.hubapi.com';
// Get the associations of a ticket (contact or company)
async function getAssociations(ticketId) {
const url = `${HUBSPOT_API_URL}/crm/v4/objects/tickets/${ticketId}/associations`;
const response = await axios.get(url, {
headers: { Authorization: `Bearer ${HUBSPOT_API_KEY}` },
});
return response.data.results;
}
// Get detailed information of a contact
async function getContactDetails(contactId) {
const url = `${HUBSPOT_API_URL}/crm/v4/objects/contacts/${contactId}`;
const response = await axios.get(url, {
headers: { Authorization: `Bearer ${HUBSPOT_API_KEY}` },
});
return response.data.properties;
}
// Get detailed information of a company
async function getCompanyDetails(companyId) {
const url = `${HUBSPOT_API_URL}/crm/v4/objects/companies/${companyId}`;
const response = await axios.get(url, {
headers: { Authorization: `Bearer ${HUBSPOT_API_KEY}` },
});
return response.data.properties;
}
// Remove association
async function removeAssociation(ticketId, assocId, assocType) {
const url = `${HUBSPOT_API_URL}/crm/v4/objects/tickets/${ticketId}/associations/${assocType}/${assocId}`;
const response = await axios.delete(url, {
headers: { Authorization: `Bearer ${HUBSPOT_API_KEY}` },
});
return response.data;
}
// Logic to process ticket automation
async function processTicket(ticketId) {
const associations = await getAssociations(ticketId);
let contactId, companyId;
// Iterate over all associations to get contact and company IDs
for (const assoc of associations) {
if (assoc.toObjectType === 'contact') {
contactId = assoc.toObjectId;
}
if (assoc.toObjectType === 'company') {
companyId = assoc.toObjectId;
}
}
// If it's a contact association
if (contactId) {
const contactDetails = await getContactDetails(contactId);
if (contactDetails.email && contactDetails.email.includes('@xxxx.com')) {
// Remove the contact association
await removeAssociation(ticketId, contactId, 'contacts');
}
}
// If it's a company association
if (companyId) {
const companyDetails = await getCompanyDetails(companyId);
const companyName = companyDetails.name ? companyDetails.name.toLowerCase() : '';
if (companyName.includes('xxxx')) {
// Remove the company association
await removeAssociation(ticketId, companyId, 'companies');
}
}
}
// Automation entry point
exports.handler = async (event) => {
const ticketId = event.ticketId; // Assuming the ticket ID is passed through the event
if (!ticketId) {
throw new Error("Ticket ID is missing from the event.");
}
await processTicket(ticketId);
return { statusCode: 200, body: 'Ticket association processed successfully' };
};
ERROR Invoke Error {"errorType":"TypeError","errorMessage":"customerPayload.main is not a function","stack":["TypeError: customerPayload.main is not a function"," at exports.hubspot_handler [as handler] (/var/task/hubspotHandler.js:7:21)"," at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)"]}
HubSpot’s AI-powered customer agent resolves up to 50% of customer queries instantly, with some customers reaching up to 90% resolution rates. Learn More.