Hi,
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;
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) => {
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}` });
}
};
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.
I am receiving the following error log:
invalid from object type 0-27 for associations to be created. expected: 0-5
According to the HubSpot documentation, the association type for linking tasks to tickets should be 230, but this doesn’t seem to work.
Can anyone advise on how to correct this?