<?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: Custom Code Workflow: Task Creation Not Associating with Ticket in APIs &amp; Integrations</title>
    <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-Code-Workflow-Task-Creation-Not-Associating-with-Ticket/m-p/1054707#M77376</link>
    <description>&lt;P&gt;I'm sorry if it seems like you ae going in a circle here! I went back to look at the related association API call and tried to cross check with your code. I' nearly always programming in Python and not using the Axios library, so I needed to do a little interpolation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The API reference for this call is at at:&amp;nbsp;&lt;A href="https://developers.hubspot.com/docs/api/crm/associations" target="_blank"&gt;https://developers.hubspot.com/docs/api/crm/associations&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;It looks like the call is a PUT type (axios.put) and requires the object information in the URL PLUS the association id(s) in the JSON data.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;PUT https://api.hubapi.com/crm/v4/objects/{from type}/{from ID}/associations/{to type}/{to ID}&lt;/LI-CODE&gt;
&lt;P&gt;The data structure is simpler - just the category and the ID, thought it seems it in a potential array format.&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;data : [
  {
    "associationCategory": "HUBSPOT_DEFINED",
    "associationTypeId": 230
  }
]&lt;/LI-CODE&gt;
&lt;P&gt;I hope the edits to your JS will be relatively simple!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Steve&lt;/P&gt;</description>
    <pubDate>Mon, 14 Oct 2024 16:00:39 GMT</pubDate>
    <dc:creator>SteveHTM</dc:creator>
    <dc:date>2024-10-14T16:00:39Z</dc:date>
    <item>
      <title>Custom Code Workflow: Task Creation Not Associating with Ticket</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-Code-Workflow-Task-Creation-Not-Associating-with-Ticket/m-p/1051566#M77203</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am setting up a workflow that, when a meeting is booked and recorded on the contact record, copies the meeting's date and time to a ticket. The next step in the workflow is to create a task assigned to the ticket owner, with a due date matching the scheduled meeting. I am implementing this using a custom code action with the following Javascript;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const hubspot = require('@hubspot/api-client');

// Create HubSpot client with access token.
const hubspotClient = new hubspot.Client({ accessToken: MY ACCESS TOKEN });

// Main function
exports.main = async (event, callback) =&amp;gt; {
    try {
        // Extract necessary fields.
        const { hubspot_owner_id: ownerId, hs_ticket_id: ticketId, latest_meeting_date___time: latestMeetingDateTime } = event.inputFields;

        // Define the task creation payload.
        const taskData = {
            properties: {
                hs_task_subject: 'Follow-up after latest meeting',
                hs_task_body: `Please follow up on the recent meeting scheduled for ${new Date(parseInt(latestMeetingDateTime)).toLocaleString()}.`,
                hs_task_status: 'NOT_STARTED',
                hs_task_priority: 'HIGH',
                hs_timestamp: parseInt(latestMeetingDateTime),
                hubspot_owner_id: parseInt(ownerId),
                hs_task_type: 'TODO'
            }
        };

        // Create the task using HubSpot's API.
        const taskResponse = await hubspotClient.crm.objects.basicApi.create('tasks', taskData);
        const taskId = taskResponse.body.id;

        // Define the association payload using the correct type ID.
        const associationPayload = {
            inputs: [{
                from: { id: taskId }, // Task ID as the "from" object.
                to: { id: ticketId }, // Ticket ID as the "to" object.
                type: 230
            }]
        };

        // Create the association between the task and the ticket using correct object types.
        await hubspotClient.crm.associations.batchApi.create('tasks', 'tickets', associationPayload);

        // Return success response.
        callback(null, { statusCode: 200, body: 'Task created and associated successfully' });
    } catch (error) {
        // Log the error and return a failure response.
        console.error('Error creating task or association:', JSON.stringify(error.response?.data || error, null, 2));
        callback(null, { statusCode: 500, body: `Error creating task or association: ${error.message}` });
    }
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, the issue I am facing is that while the task is being created and appears in my task list, it is not being associated with the ticket enrolled in the workflow.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am receiving the following error log:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;invalid from object type 0-27 for associations to be created. expected: 0-5&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;According to the &lt;A href="https://developers.hubspot.com/docs/api/crm/associations#association-type-id-values" target="_new" rel="noopener"&gt;&lt;SPAN&gt;HubSpot&lt;/SPAN&gt;&lt;SPAN&gt; documentation&lt;/SPAN&gt;&lt;/A&gt;, the association type for linking tasks to tickets should be 230, but this doesn't seem to work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone advise on how to correct this?&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2024 12:03:11 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Custom-Code-Workflow-Task-Creation-Not-Associating-with-Ticket/m-p/1051566#M77203</guid>
      <dc:creator>DJohnson4</dc:creator>
      <dc:date>2024-10-08T12:03:11Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Code Workflow: Task Creation Not Associating with Ticket</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-Code-Workflow-Task-Creation-Not-Associating-with-Ticket/m-p/1051654#M77206</link>
      <description>&lt;P&gt;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/121959"&gt;@DJohnson4&lt;/a&gt;&amp;nbsp;- if you want to understand the full set of association type options available at any given time, there is no better call to check this than the association schema call:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://api.hubapi.com/crm/v4/associations/{fromObjectType}/{toObjectType}/labels" target="_blank"&gt;https://api.hubapi.com/crm/v4/associations/{fromObjectType}/{toObjectType}/labels&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In fact I typically call this when manipulating associations rather than trust the written documentation - and it also reports the extra user definitions that could be added at any time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this case 230 does get reported as the default ID value - so you may be no further forward. But I did think it was worth checking.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In doing this I reviewed my own code for creating an association and have a different style of payload for a direct call (not using HubSpot library). The current documentation is strangley for the v1 calls - whereas I have been using v4 calls, but perhaps the missing element of the JSON structure is the HUBSPOT_DEFINED category?&lt;/P&gt;
&lt;P&gt;&lt;A href="https://api.hubapi.com/crm-associations/v1/associations" target="_blank"&gt;https://api.hubapi.com/crm-associations/v1/associations&lt;/A&gt;&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;Example PUT JSON:
{
  "fromObjectId": 496346,
  "toObjectId": 176602,
  "category": "HUBSPOT_DEFINED",
  "definitionId": 15
}
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Steve&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Steve&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2024 14:03:23 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Custom-Code-Workflow-Task-Creation-Not-Associating-with-Ticket/m-p/1051654#M77206</guid>
      <dc:creator>SteveHTM</dc:creator>
      <dc:date>2024-10-08T14:03:23Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Code Workflow: Task Creation Not Associating with Ticket</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-Code-Workflow-Task-Creation-Not-Associating-with-Ticket/m-p/1054682#M77371</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/63499"&gt;@SteveHTM&lt;/a&gt;,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I am currently running this script&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const axios = require('axios');

const accessToken = 'MY ACCESS TOKEN';

// Main function
exports.main = async (event, callback) =&amp;gt; {
    try {
        // Step 1: Extract input fields from the workflow
        const ownerId = event.inputFields['hubspot_owner_id'];
        const ticketId = event.inputFields['hs_ticket_id'];
        const latestMeetingDateTime = event.inputFields['latest_meeting_date___time'];

        // Step 2: Define the task creation payload
        const taskData = {
            properties: {
                hs_task_subject: 'Follow-up after latest meeting',
                hs_task_body: 'Please follow up on the recent meeting.',
                hs_task_status: 'NOT_STARTED',
                hs_task_priority: 'HIGH',
                hs_timestamp: parseInt(latestMeetingDateTime),
                hubspot_owner_id: parseInt(ownerId),
                hs_task_type: 'TODO'
            }
        };

        // Step 3: Create the task using HubSpot API
        const taskResponse = await axios.post('https://api.hubapi.com/crm/v3/objects/tasks', taskData, {
            headers: {
                Authorization: `Bearer ${accessToken}`,
                'Content-Type': 'application/json'
            }
        });

        const taskId = taskResponse.data.id;  // Get the created task ID
        console.log('Task created successfully:', taskId);

        // Step 4: Associate the task with the ticket
        const associationData = {
            fromObjectId: taskId,
            toObjectId: ticketId,
            category: 'HUBSPOT_DEFINED',
            definitionId: 230  // The correct association type ID
        };

        const associationResponse = await axios.post('https://api.hubapi.com/crm/v4/objects/associations', associationData, {
            headers: {
                Authorization: `Bearer ${accessToken}`,
                'Content-Type': 'application/json'
            }
        });

        console.log('Task associated successfully with the ticket:', associationResponse.data);

        // Step 5: Return success response to the workflow
        callback(null, { taskId: taskId });

    } catch (error) {
        // Log the detailed error response if available
        if (error.response) {
            console.error('Error Response Data:', JSON.stringify(error.response.data, null, 2));
        }
        console.error('Error creating task or associating it:', error.message);
        callback(error);  // Return the error if any occurs
    }
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;This seems to have no issues creating the task, but is unable to create the association. I have been going back and forth with ChatGPT trying to figure this out, but i'm getting the same result everytime. This is the error log:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="css"&gt;2024-10-14T15:28:17.679Z	INFO	Task created successfully: 62272564837
2024-10-14T15:28:17.837Z	ERROR	Error Response Data: {
  "status": "error",
  "message": "Unable to infer object type from: associations",
  "correlationId": "033d080c-1c6d-4327-ad16-a3e57bd50c11"
}
2024-10-14T15:28:17.837Z	ERROR	Error creating task or associating it: Request failed with status code 400

Memory: 82/128 MB
Runtime: 2002.60 ms&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Apologies, I am not a developer and have little to no experience with javascript, so I'm completely stumped by this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;David&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2024 15:35:47 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Custom-Code-Workflow-Task-Creation-Not-Associating-with-Ticket/m-p/1054682#M77371</guid>
      <dc:creator>DJohnson4</dc:creator>
      <dc:date>2024-10-14T15:35:47Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Code Workflow: Task Creation Not Associating with Ticket</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-Code-Workflow-Task-Creation-Not-Associating-with-Ticket/m-p/1054707#M77376</link>
      <description>&lt;P&gt;I'm sorry if it seems like you ae going in a circle here! I went back to look at the related association API call and tried to cross check with your code. I' nearly always programming in Python and not using the Axios library, so I needed to do a little interpolation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The API reference for this call is at at:&amp;nbsp;&lt;A href="https://developers.hubspot.com/docs/api/crm/associations" target="_blank"&gt;https://developers.hubspot.com/docs/api/crm/associations&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;It looks like the call is a PUT type (axios.put) and requires the object information in the URL PLUS the association id(s) in the JSON data.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;PUT https://api.hubapi.com/crm/v4/objects/{from type}/{from ID}/associations/{to type}/{to ID}&lt;/LI-CODE&gt;
&lt;P&gt;The data structure is simpler - just the category and the ID, thought it seems it in a potential array format.&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;data : [
  {
    "associationCategory": "HUBSPOT_DEFINED",
    "associationTypeId": 230
  }
]&lt;/LI-CODE&gt;
&lt;P&gt;I hope the edits to your JS will be relatively simple!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Steve&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2024 16:00:39 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Custom-Code-Workflow-Task-Creation-Not-Associating-with-Ticket/m-p/1054707#M77376</guid>
      <dc:creator>SteveHTM</dc:creator>
      <dc:date>2024-10-14T16:00:39Z</dc:date>
    </item>
    <item>
      <title>Re: Custom Code Workflow: Task Creation Not Associating with Ticket</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-Code-Workflow-Task-Creation-Not-Associating-with-Ticket/m-p/1055180#M77401</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/63499"&gt;@SteveHTM&lt;/a&gt;, Thanks so much for your help.&lt;BR /&gt;&lt;BR /&gt;I managed to get it working by changing the HTTP method from POST to PUT,&amp;nbsp; including the object types and their respective ID in the URL and structuring the association data in an array format.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Once again, thank you!&lt;/P&gt;&lt;P&gt;David&lt;/P&gt;</description>
      <pubDate>Tue, 15 Oct 2024 12:36:12 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Custom-Code-Workflow-Task-Creation-Not-Associating-with-Ticket/m-p/1055180#M77401</guid>
      <dc:creator>DJohnson4</dc:creator>
      <dc:date>2024-10-15T12:36:12Z</dc:date>
    </item>
  </channel>
</rss>

