APIs & Integrations

VaanLeung
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)"]}

Memory: 74/128 MB
Runtime: 55.47 ms

I kept asking during the test, what should I do?

1 Accepted solution
sylvain_tirreau
Solution
Contributor

automation from tickets to remove Contact & Companies

SOLVE

Hi,

It works if you try this?

exports.main = exports.handler;

 Best

View solution in original post

2 Replies 2
sylvain_tirreau
Solution
Contributor

automation from tickets to remove Contact & Companies

SOLVE

Hi,

It works if you try this?

exports.main = exports.handler;

 Best

BérangèreL
Community Manager
Community Manager

automation from tickets to remove Contact & Companies

SOLVE

Hi @VaanLeung and welcome, it's a pleasure to have you here!

Thanks for asking the HubSpot Community!

I understand that you are trying to remove the associations from the tickets. Please let me know if that's not the case.

I'd like to share these resources that might be of interest:

- Ticket API
- The solution from @Probley on this post "Bulk removal of associations to a ticket"

I'd love to put you in touch with some of our Top Experts: Hi @sylvain_tirreau, @zach_threadint and @Anton do you have suggestions to help @VaanLeung, please?

Have a lovely weekend and thanks so much!

Best,
Bérangère


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.


Saviez vous que la Communauté est disponible en français?
Rejoignez les discussions francophones en changeant votre langue dans les paramètres! !
0 Upvotes