<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic automation from tickets to remove Contact &amp;amp; Companies in APIs &amp; Integrations</title>
    <link>https://community.hubspot.com/t5/APIs-Integrations/automation-from-tickets-to-remove-Contact-amp-Companies/m-p/1124772#M81059</link>
    <description>&lt;P&gt;Hi Mate,I'm a rookie who just started using Hubspot.&lt;/P&gt;&lt;P&gt;I'm trying to mate a funtion to&amp;nbsp;automation from tickets to remove Contact &amp;amp; Companies,but now my code still have some&amp;nbsp;Invoke Error ,May I ask your help?&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;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 &amp;amp;&amp;amp; 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) =&amp;gt; {
  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' };
};&lt;/LI-CODE&gt;&lt;P&gt;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)"]}&lt;/P&gt;&lt;P&gt;Memory: 74/128 MB&lt;BR /&gt;Runtime: 55.47 ms&lt;/P&gt;&lt;P&gt;I kept asking during the test, what should I do?&lt;/P&gt;</description>
    <pubDate>Thu, 20 Mar 2025 04:00:37 GMT</pubDate>
    <dc:creator>VaanLeung</dc:creator>
    <dc:date>2025-03-20T04:00:37Z</dc:date>
    <item>
      <title>automation from tickets to remove Contact &amp; Companies</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/automation-from-tickets-to-remove-Contact-amp-Companies/m-p/1124772#M81059</link>
      <description>&lt;P&gt;Hi Mate,I'm a rookie who just started using Hubspot.&lt;/P&gt;&lt;P&gt;I'm trying to mate a funtion to&amp;nbsp;automation from tickets to remove Contact &amp;amp; Companies,but now my code still have some&amp;nbsp;Invoke Error ,May I ask your help?&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;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 &amp;amp;&amp;amp; 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) =&amp;gt; {
  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' };
};&lt;/LI-CODE&gt;&lt;P&gt;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)"]}&lt;/P&gt;&lt;P&gt;Memory: 74/128 MB&lt;BR /&gt;Runtime: 55.47 ms&lt;/P&gt;&lt;P&gt;I kept asking during the test, what should I do?&lt;/P&gt;</description>
      <pubDate>Thu, 20 Mar 2025 04:00:37 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/automation-from-tickets-to-remove-Contact-amp-Companies/m-p/1124772#M81059</guid>
      <dc:creator>VaanLeung</dc:creator>
      <dc:date>2025-03-20T04:00:37Z</dc:date>
    </item>
    <item>
      <title>Re: automation from tickets to remove Contact &amp; Companies</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/automation-from-tickets-to-remove-Contact-amp-Companies/m-p/1125415#M81061</link>
      <description>&lt;P&gt;Hi &lt;SPAN style="background: var(--ck-color-mention-background); color: var(--ck-color-mention-text);"&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/917017"&gt;@VaanLeung&lt;/a&gt;&lt;/SPAN&gt; and welcome, it's a pleasure to have you here!&lt;BR /&gt;&lt;BR /&gt;Thanks for asking the HubSpot Community!&lt;BR /&gt;&lt;BR /&gt;I understand that you are trying to remove the associations from the tickets. Please let me know if that's not the case.&lt;BR /&gt;&lt;BR /&gt;I'd like to share these resources that might be of interest:&lt;BR /&gt;&lt;BR /&gt;- &lt;A href="https://developers.hubspot.com/docs/guides/api/crm/objects/tickets#:~:text=To%20remove%20an%20association%20between,toObjectType%7D%2F%7BtoObjectId%7D%2F%7BassociationTypeId%7D%20." target="_blank"&gt;Ticket API&lt;/A&gt;&lt;BR /&gt;- The solution from &lt;SPAN style="background: var(--ck-color-mention-background); color: var(--ck-color-mention-text);"&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/617765"&gt;@Probley&lt;/a&gt;&lt;/SPAN&gt; on this post "&lt;A href="https://community.hubspot.com/t5/APIs-Integrations/Bulk-removal-of-associations-to-a-ticket/m-p/952579" target="_blank"&gt;Bulk removal of associations to a ticket&lt;/A&gt;"&lt;BR /&gt;&lt;BR /&gt;I'd love to put you in touch with some of our Top Experts: Hi &lt;SPAN style="background: var(--ck-color-mention-background); color: var(--ck-color-mention-text);"&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/601366"&gt;@sylvain_tirreau&lt;/a&gt;&lt;/SPAN&gt;, &lt;SPAN style="background: var(--ck-color-mention-background); color: var(--ck-color-mention-text);"&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/324811"&gt;@zach_threadint&lt;/a&gt;&lt;/SPAN&gt; and &lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/17186"&gt;@Anton&lt;/a&gt; do you have suggestions to help &lt;SPAN style="background: var(--ck-color-mention-background); color: var(--ck-color-mention-text);"&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/917017"&gt;@VaanLeung&lt;/a&gt;&lt;/SPAN&gt;, please?&lt;BR /&gt;&lt;BR /&gt;Have a lovely weekend and thanks so much!&lt;BR /&gt;&lt;BR /&gt;Best,&lt;BR /&gt;Bérangère&lt;/P&gt;</description>
      <pubDate>Fri, 21 Mar 2025 08:16:28 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/automation-from-tickets-to-remove-Contact-amp-Companies/m-p/1125415#M81061</guid>
      <dc:creator>BérangèreL</dc:creator>
      <dc:date>2025-03-21T08:16:28Z</dc:date>
    </item>
    <item>
      <title>Re: automation from tickets to remove Contact &amp; Companies</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/automation-from-tickets-to-remove-Contact-amp-Companies/m-p/1129663#M81268</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;It works if you try this?&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;exports.main = exports.handler;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;Best&lt;/P&gt;</description>
      <pubDate>Mon, 31 Mar 2025 10:43:33 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/automation-from-tickets-to-remove-Contact-amp-Companies/m-p/1129663#M81268</guid>
      <dc:creator>sylvain_tirreau</dc:creator>
      <dc:date>2025-03-31T10:43:33Z</dc:date>
    </item>
  </channel>
</rss>

