Hello everyone,
I am creating a custom code for a workflow because we need to create a task with the task’s due date being a date property of the deal. Right now, I got that working but the task is not associated with the deal upon creation. I’m following this documentation: Accounts Dashboard | HubSpot
Here’s the code I’m using:
const hubspot = require('@hubspot/api-client');
exports.main = async (event, callback) => {
const hubspotClient = new hubspot.Client({
accessToken: ACCESS_TOKEN
});
try {
await createTaskFromDeal(hubspotClient, event);
console.log('Task created successfully');
callback(null, 'Task created successfully');
} catch (error) {
console.error('Error creating task:', error.message);
callback(error);
}
};
// Get the deal properties and create the task
async function createTaskFromDeal(hubspotClient, event) {
const dealId = event.object.objectId;
console.log('dealId:', dealId);
const results = await hubspotClient.crm.deals.basicApi.getById(dealId, [
'dealname',
'pvod_project_due_date'
]);
const dealName = results.properties['dealname'];
const dueDate = results.properties['pvod_project_due_date'];
console.log(dealName);
console.log(dueDate);
await createTask(hubspotClient, dealName, dueDate, dealId);
}
// Function to create a task in HubSpot
async function createTask(hubspotClient, dealName, dueDate, dealId) {
const taskData = {
hs_task_subject: "Capture",
hs_task_type: "TODO",
hs_task_priority: "HIGH",
hs_task_status: "NOT_STARTED",
hubspot_owner_id: "345508386",
hs_task_body: dealName,
hs_timestamp: new Date(dueDate).toISOString() // Format the dueDate as ISO string
};
const SimplePublicObjectInputForCreate = {
properties: taskData,
associations: [
{
to:{
id:dealId
},
types:[
{
associationCategory:"HUBSPOT_DEFINED",
associationTypeId:12
}
]
}
]
};
try {
await hubspotClient.crm.objects.tasks.basicApi.create(SimplePublicObjectInputForCreate);
} catch (error) {
throw new Error('Error creating task: ' + error.message);
}
}
Any insight regarding the associations would be highly appreciated!