<?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 Re: bulk deleting associated emails from a company record in APIs &amp; Integrations</title>
    <link>https://community.hubspot.com/t5/APIs-Integrations/bulk-deleting-associated-emails-from-a-company-record/m-p/1042915#M76729</link>
    <description>&lt;P&gt;Hey, &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/164274"&gt;@MatBehr&lt;/a&gt;&lt;/SPAN&gt; &lt;span class="lia-unicode-emoji" title=":waving_hand:"&gt;👋&lt;/span&gt; Can you tell our community more about &lt;I&gt;where&lt;/I&gt; your code is breaking? Does it partially work? Or is it throwing errors immediately? Also, can you share the full error messages you get when running this code, please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks! — Jaycee&lt;/P&gt;</description>
    <pubDate>Wed, 18 Sep 2024 23:27:10 GMT</pubDate>
    <dc:creator>Jaycee_Lewis</dc:creator>
    <dc:date>2024-09-18T23:27:10Z</dc:date>
    <item>
      <title>bulk deleting associated emails from a company record</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/bulk-deleting-associated-emails-from-a-company-record/m-p/1042586#M76709</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;Im trying to bulk remove the associtaed emails from a company record as we have hit the 50000 limit, im usuing the following code but cant seem to get it to work correctly,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;const hubspot = require('@hubspot/api-client');&lt;/P&gt;&lt;P&gt;exports.main = async (event) =&amp;gt; {&lt;BR /&gt;const token = process.env.Company_token;&lt;BR /&gt;const companyId = event.inputFields.hs_object_id;&lt;BR /&gt;console.log('Using company ID:', companyId);&lt;/P&gt;&lt;P&gt;// Initialize the HubSpot client&lt;BR /&gt;const hubspotClient = new hubspot.Client({ accessToken: token });&lt;/P&gt;&lt;P&gt;// Step 1: Get all engagements associated with the company&lt;BR /&gt;async function getEngagementsForCompany(companyId) {&lt;BR /&gt;try {&lt;BR /&gt;const response = await hubspotClient.crm.objects.searchApi.doSearch('engagements', {&lt;BR /&gt;filterGroups: [{&lt;BR /&gt;filters: [{&lt;BR /&gt;propertyName: 'associations.company',&lt;BR /&gt;operator: 'EQ',&lt;BR /&gt;value: companyId&lt;BR /&gt;}]&lt;BR /&gt;}],&lt;BR /&gt;properties: ['email.id'], //email ID&lt;BR /&gt;});&lt;/P&gt;&lt;P&gt;console.log('Using emailid:', email.id);&lt;/P&gt;&lt;P&gt;// Step 2: Delete the association between the email and the company&lt;BR /&gt;async function deleteAssociation(emailId, companyId) {&lt;BR /&gt;const associationTypeId = 'engagement_to_company'; // Replace with the actual association type ID if different&lt;/P&gt;&lt;P&gt;try {&lt;BR /&gt;await hubspotClient.apiRequest({&lt;BR /&gt;method: 'DELETE',&lt;BR /&gt;path: `/crm/v3/objects/emails/${emailId}/associations/company/${companyId}/${associationTypeId}`,&lt;BR /&gt;headers: {&lt;BR /&gt;'Authorization': `Bearer ${token}`,&lt;BR /&gt;'Content-Type': 'application/json',&lt;BR /&gt;}&lt;BR /&gt;});&lt;BR /&gt;console.log(`Successfully deleted association for email ${emailId} with company ${companyId}`);&lt;BR /&gt;} catch (error) {&lt;BR /&gt;console.error(`Error deleting association: ${error.response?.status} - ${error.message}`);&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Step 3: Main function to process email unassociation&lt;BR /&gt;async function removeEmailAssociationsFromCompany(companyId) {&lt;BR /&gt;const engagements = await getEngagementsForCompany(companyId);&lt;/P&gt;&lt;P&gt;for (const engagement of engagements) {&lt;BR /&gt;const engagementId = engagement.id;&lt;BR /&gt;const engagementType = engagement.properties?.['engagement.type'];&lt;BR /&gt;const emailId = engagement.properties?.['email.id']; // Extract email ID&lt;/P&gt;&lt;P&gt;// Log engagement details for debugging&lt;BR /&gt;console.log(`Processing engagement ${engagementId} with type ${engagementType} and email ID ${emailId}`);&lt;/P&gt;&lt;P&gt;// Only handle email engagements and ensure email ID is present&lt;BR /&gt;if (engagementType === 'EMAIL' &amp;amp;&amp;amp; emailId) {&lt;BR /&gt;await deleteAssociation(emailId, companyId);&lt;BR /&gt;}&lt;BR /&gt;}&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Execute the script&lt;BR /&gt;await removeEmailAssociationsFromCompany(companyId);&lt;BR /&gt;};&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2024 12:23:46 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/bulk-deleting-associated-emails-from-a-company-record/m-p/1042586#M76709</guid>
      <dc:creator>MatBehr</dc:creator>
      <dc:date>2024-09-18T12:23:46Z</dc:date>
    </item>
    <item>
      <title>Re: bulk deleting associated emails from a company record</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/bulk-deleting-associated-emails-from-a-company-record/m-p/1042915#M76729</link>
      <description>&lt;P&gt;Hey, &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/164274"&gt;@MatBehr&lt;/a&gt;&lt;/SPAN&gt; &lt;span class="lia-unicode-emoji" title=":waving_hand:"&gt;👋&lt;/span&gt; Can you tell our community more about &lt;I&gt;where&lt;/I&gt; your code is breaking? Does it partially work? Or is it throwing errors immediately? Also, can you share the full error messages you get when running this code, please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks! — Jaycee&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2024 23:27:10 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/bulk-deleting-associated-emails-from-a-company-record/m-p/1042915#M76729</guid>
      <dc:creator>Jaycee_Lewis</dc:creator>
      <dc:date>2024-09-18T23:27:10Z</dc:date>
    </item>
    <item>
      <title>Re: bulk deleting associated emails from a company record</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/bulk-deleting-associated-emails-from-a-company-record/m-p/1043057#M76744</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Error is as follows,&amp;nbsp;&lt;/P&gt;&lt;P&gt;WARNING: The logs for this function have exceeded the 4KB limit.&lt;BR /&gt;...&lt;BR /&gt;2024-09-19T08:23:08.451Z undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Missing catch or finally after try","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Missing catch or finally after try"," at _loadUserApp (file:///var/runtime/index.mjs:1084:17)"," at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)"," at async start (file:///var/runtime/index.mjs:1282:23)"," at async file:///var/runtime/index.mjs:1288:1"]}&lt;BR /&gt;INIT_REPORT Init Duration: 154.51 ms Phase: init Status: error Error Type: Runtime.UserCodeSyntaxError&lt;BR /&gt;2024-09-19T08:23:09.568Z undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Missing catch or finally after try","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Missing catch or finally after try"," at _loadUserApp (file:///var/runtime/index.mjs:1084:17)"," at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)"," at async start (file:///var/runtime/index.mjs:1282:23)"," at async file:///var/runtime/index.mjs:1288:1"]}&lt;BR /&gt;INIT_REPORT Init Duration: 1359.27 ms Phase: invoke Status: error Error Type: Runtime.UserCodeSyntaxError&lt;BR /&gt;START RequestId: 114a2668-2bb0-43e2-9b5e-52ba7bed1b58 Version: $LATEST&lt;/P&gt;&lt;P&gt;Memory: 65/128 MB&lt;BR /&gt;Runtime: 1369.92 ms&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;All im really trying to do, is find all email engagements that are associated to the enrolled company and delete the asociation as we have hit the 50000 email to company associtaion limit but emails dont need to be associated with the company&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2024 08:27:31 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/bulk-deleting-associated-emails-from-a-company-record/m-p/1043057#M76744</guid>
      <dc:creator>MatBehr</dc:creator>
      <dc:date>2024-09-19T08:27:31Z</dc:date>
    </item>
  </channel>
</rss>

