# How to associate Task with Custom object?

**URL:** <https://community.hubspot.com/t/how-to-associate-task-with-custom-object/116646>\
**Category:** APIs & Integrations\
**Tags:** php\
**Created:** [September 19, 2024, 7:56am UTC](https://community.hubspot.com/t/how-to-associate-task-with-custom-object/116646 "2024-09-19T07:56:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![KPadhiyar](https://avatars.discourse-cdn.com/v4/letter/k/7993a0/32.png) [@KPadhiyar](https://community.hubspot.com/u/KPadhiyar)\
**Post date:** [September 19, 2024, 7:56am UTC](https://community.hubspot.com/t/how-to-associate-task-with-custom-object/116646/1 "2024-09-19T07:56:03Z")

</div>

I’ve to attach task with my custom object. I’m using below code as defined in docs. But I can’t able to create it and my object name is ‘test’.

```lia-code-sample
I've created Association class in PHP:
Association::create($from, $to, $type, $fromObjectType, $toObjectType);

$from => task_id
$to => custom_object_id
$type => I've custom object name test so I passed test_to_task
I don't have idea about $fromObjectType, $toObjectType.

Where create method contains below code:
public function create($from, $to, $type, $fromObjectType, $toObjectType)
 {
 $fromId = new PublicObjectId([
 'id' => $from,
 ]);

 $toId = new PublicObjectId([
 'id' => $to,
 ]);

 $publicAssociation1 = new PublicAssociation([
 'from' => $fromId,
 'to' => $toId,
 'type' => $type,
 ]);

 $batchInputPublicAssociation = new BatchInputPublicAssociation([
 'inputs' => [$publicAssociation1],
 ]);

 return $hubSpot->crm()->associations()->batchApi()->create($fromObjectType, $toObjectType, $batchInputPublicAssociation);
 }

Where $hubSpot is:
Factory::createWithAccessToken('access_token');

```

---

<div class="post-metadata">

**Author:** ![Teun](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/teun/32/28739_2.png) [@Teun](https://community.hubspot.com/u/Teun)\
**Post date:** [September 19, 2024, 8:43pm UTC](https://community.hubspot.com/t/how-to-associate-task-with-custom-object/116646/2 "2024-09-19T20:43:00Z")

</div>

Hi @KPadhiyar ,

When making an API call to create an association, the fromObjectType should contain the internal name of the object, so ‘contact’ or ‘company’, the same goes for the toObjectType.  
So in your case, you can use ‘task’ and ‘test’ for the two variables.

---

<div class="post-metadata">

**Author:** ![KPadhiyar](https://avatars.discourse-cdn.com/v4/letter/k/7993a0/32.png) [@KPadhiyar](https://community.hubspot.com/u/KPadhiyar)\
**Post date:** [September 20, 2024, 3:58am UTC](https://community.hubspot.com/t/how-to-associate-task-with-custom-object/116646/3 "2024-09-20T03:58:19Z")

</div>

Hi @Teun, Thanks for the answer. I’ve done same thing before posting here. But its gives me error like:

[400] Client error: `POST [https://api.hubapi.com/crm/v3/associations/task/test/batch/create\`](https://api.hubapi.com/crm/v3/associations/task/test/batch/create%5C%60) resulted in a ` 400 Bad Request` response:  
{“status”:“error”,“message”:“Unable to infer object type from: test”.  
I’m using below code:  
Association::make($hubSpotAccount)-\>create(  
task\_id,  
test\_object object\_id,  
“test\_to\_task”,  
“task”,  
“test”  
);

---

<div class="post-metadata">

**Author:** ![KPadhiyar](https://avatars.discourse-cdn.com/v4/letter/k/7993a0/32.png) [@KPadhiyar](https://community.hubspot.com/u/KPadhiyar)\
**Post date:** [October 3, 2024, 9:27am UTC](https://community.hubspot.com/t/how-to-associate-task-with-custom-object/116646/4 "2024-10-03T09:27:22Z")

</div>

I’m still finding solution for above.  
Thanks!
