# Associate Task to Deal when creating a task through HubSpot API

**URL:** <https://community.hubspot.com/t/associate-task-to-deal-when-creating-a-task-through-hubspot-api/82966>\
**Category:** APIs & Integrations\
**Tags:** object\
**Created:** [June 8, 2023, 4:18pm UTC](https://community.hubspot.com/t/associate-task-to-deal-when-creating-a-task-through-hubspot-api/82966 "2023-06-08T16:18:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zCamacaro](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/zcamacaro/32/41647_2.png) [@zCamacaro](https://community.hubspot.com/u/zCamacaro)\
**Post date:** [June 8, 2023, 4:18pm UTC](https://community.hubspot.com/t/associate-task-to-deal-when-creating-a-task-through-hubspot-api/82966/1 "2023-06-08T16:18:27Z")

</div>

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](https://developers.hubspot.com/docs/api/crm/tasks)

Here’s the code I’m using:

```lia-code-sample
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!

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [June 9, 2023, 7:54pm UTC](https://community.hubspot.com/t/associate-task-to-deal-when-creating-a-task-through-hubspot-api/82966/2 "2023-06-09T19:54:49Z")

</div>

Hey, @zCamacaro One quick question. Can you check the association ID for Tasks \> Deals here, please?

`GET crm/v4/associations/tasks/deal/labels`

When I make the request in my test portal for Task to Deal, I get the response of

```lia-code-sample
{
 "results": [
 {
 "category": "HUBSPOT_DEFINED",
 "typeId": 216,
 "label": null
 }
 ]
}

```

And because you have me curious, Deal to Task is returned as

```lia-code-sample
GET /crm/v4/associations/deal/task/labels

{
 "results": [
 {
 "category": "HUBSPOT_DEFINED",
 "typeId": 215,
 "label": null
 }
 ]
}

```

I hope this helps get you moving forward! — Jaycee
