# associations can't be created by custom code in workflow

**URL:** <https://community.hubspot.com/t/associations-cant-be-created-by-custom-code-in-workflow/91558>\
**Category:** APIs & Integrations\
**Tags:** workflows\
**Created:** [October 11, 2023, 11:24am UTC](https://community.hubspot.com/t/associations-cant-be-created-by-custom-code-in-workflow/91558 "2023-10-11T11:24:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anonymous\_18ff7252298d6d3ae1b7c1cde37784a0](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Anonymous\_18ff7252298d6d3ae1b7c1cde37784a0](https://community.hubspot.com/u/Anonymous_18ff7252298d6d3ae1b7c1cde37784a0)\
**Post date:** [October 11, 2023, 11:24am UTC](https://community.hubspot.com/t/associations-cant-be-created-by-custom-code-in-workflow/91558/1 "2023-10-11T11:24:45Z")

</div>

Dear Community

I want to use API(hubspotClient.crm.objects.basicApi.create) to create associations in workflow custome code. but object was created,associations was not created use the following code

```lia-code-sample
 const properties = {
 "dealname_lot": "BB",
 "deal_stage_copy": "75418027",
 "hubspot_owner_id": "554201143",
 "country": "Philippines",
 "hs_pipeline": "42134661",
 "hs_pipeline_stage": "88330293",
 "sales_lot_amount": "1000",
 "sales_record_month": "2023-10-11"
 };
 const SimplePublicObjectInputForCreate = { associations: [{"types":[{"associationCategory":"USER_DEFINED","associationTypeId":17}],"to":{"id":"15390771024"}}], properties };
 const objectType = "2-18983127";
 await hubspotClient.crm.objects.basicApi.create(objectType, SimplePublicObjectInputForCreate);<div><div><span> <span>When <span>i <span>use <span>associations <span>API<span>-<span>V4<span>, <span>the <span>following <span>error <span>was <span>occurred.<li-code lang="javascript">const BatchInputPublicAssociationMultiPost = { inputs: [{"types":[{"associationCategory":"USER_DEFINED","associationTypeId":17}],"_from":{"id":"9344806988"},"to":{"id":"15390771024"}}] };
const fromObjectType = "2-18983127";
const toObjectType = "0-3";
const apiResponse = await hubspotClient.crm.associations.v4.batchApi.create(fromObjectType, toObjectType, BatchInputPublicAssociationMultiPost);
{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: Cannot read properties of undefined (reading 'basicApi')","reason":{"errorType":"TypeError","errorMessage":"Cannot read properties of undefined (reading 'basicApi')","stack":["TypeError: Cannot read properties of undefined (reading 'basicApi')"," at Object.exports.main (/var/task/file.js:47:43)"," at Runtime.exports.hubspot_handler [as handler] (/var/task/hubspotHandler.js:6:21)"," at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1147:29)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: TypeError: Cannot read properties of undefined (reading 'basicApi')"," at process.<anonymous> (file:///var/runtime/index.mjs:1250:17)"," at process.emit (node:events:513:28)"," at emit (node:internal/process/promises:140:20)"," at processPromiseRejections (node:internal/process/promises:274:27)"," at processTicksAndRejections (node:internal/process/task_queues:97:32)"]}<div><div><span>When <span>i <span>use <span>associations <span>API<span>-<span>V3<span>, <span>the <span>associations <span>can <span>be <span>created <span><span>successfully.<li-code lang="javascript">const BatchInputPublicAssociation = { inputs: [{"_from":{"id":"9346258577"},"to":{"id":"15390771024"},"type":"17"}] };
const fromObjectType = "2-18983127";
const toObjectType = "0-3";
const apiResponse = await hubspotClient.crm.associations.batchApi.create(fromObjectType, toObjectType, BatchInputPublicAssociation);
 <p>how can i use <span>API<span>(<span>hubspotClient<span>.<span>crm<span>.<span>objects<span>.<span>basicApi<span>.<span>create<span>) <span>to <span>create <span>associations <span>in <span>workflow <span>custome <span>code?</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p>

```

---

<div class="post-metadata">

**Author:** ![zachh](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/zachh/32/87065_2.png) [@zachh](https://community.hubspot.com/u/zachh)\
**Post date:** [October 11, 2023, 4:21pm UTC](https://community.hubspot.com/t/associations-cant-be-created-by-custom-code-in-workflow/91558/2 "2023-10-11T16:21:06Z")

</div>

Hi [@張志明](https://community.hubspot.com/t5/user/viewprofilepage/user-id/641822),

It looks like some of your code block includes comments that were intended to be in your post and it’s making it a bit challenging to parse:

![zachh_0-1697040064619.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/6/6/666a68b503b94b2d3fae15d387597e1e6f5c369d.png)

It looks like you’re using the batch endpoint to connect companies and deals and assign association labels. Is this correct? Are you trying to associate one record with another single record, or are you associating multiple records at the same time?

How is your workflow set up?

EDIT:

You may need to define some of these. You have this:

`await hubspotClient.crm.objects.basicApi.create`

However, you don’t have anything above it defining it. Many of the error messages in your code example say “Cannot read properties of undefined” so this may be a factor. If you haven’t removed it to hide your API key, you can add:

const hubspot = require(‘@hubspot/api-client’);

const hubspotClient = new hubspot.Client({“accessToken”:“YOUR\_ACCESS\_TOKEN”});

- Zach

---

<div class="post-metadata">

**Author:** ![Anonymous\_18ff7252298d6d3ae1b7c1cde37784a0](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Anonymous\_18ff7252298d6d3ae1b7c1cde37784a0](https://community.hubspot.com/u/Anonymous_18ff7252298d6d3ae1b7c1cde37784a0)\
**Post date:** [October 12, 2023, 1:39am UTC](https://community.hubspot.com/t/associations-cant-be-created-by-custom-code-in-workflow/91558/3 "2023-10-12T01:39:14Z")

</div>

dear @zachh

thanks very much for your reply. 🙂

I use API(node.js method -- crm.objects.basicApi.create) to create [custom objects] and association with a deal sucessfully in [HubSpot API Docs Test Platform]

 ![_1-1697074181369.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/d/da455867ed7c810bfba7f1085cd702caa3215890.png)

When i copy the node.js code to WorkFlow’s custom code, it can be execute successfully and [custom object] was created, but association with deal was not created.

what’s the reason?

```lia-code-sample
const hubspot = require('@hubspot/api-client');

exports.main = async (event, callback) => {

 const hubspotClient = new hubspot.Client({"accessToken":process.env.SalesLotApi});
 const properties = {
 "dealname_lot": "BB",
 "deal_stage_copy": "75418027",
 "hubspot_owner_id": "554201143",
 "country": "Philippines",
 "hs_pipeline": "42134661",
 "hs_pipeline_stage": "88330293",
 "sales_lot_amount": "1000",
 "sales_record_month": "2023-10-11"
 };
 const SimplePublicObjectInputForCreate = { associations: [{"types":[{"associationCategory":"USER_DEFINED","associationTypeId":17}],"to":{"id":"15390771024"}}], properties };
 const objectType = "2-18983127";
 await hubspotClient.crm.objects.basicApi.create(objectType, SimplePublicObjectInputForCreate);
}

```

---

<div class="post-metadata">

**Author:** ![Anonymous\_18ff7252298d6d3ae1b7c1cde37784a0](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Anonymous\_18ff7252298d6d3ae1b7c1cde37784a0](https://community.hubspot.com/u/Anonymous_18ff7252298d6d3ae1b7c1cde37784a0)\
**Post date:** [October 12, 2023, 2:24am UTC](https://community.hubspot.com/t/associations-cant-be-created-by-custom-code-in-workflow/91558/4 "2023-10-12T02:24:42Z")

</div>

@zachh

When i use associations API-V4([Accounts Dashboard | HubSpot](https://developers.hubspot.com/docs/api/crm/associations)) to create a association, it can be creat successfully in [HubSpot API Docs Test Platform].

When copy the node.js code to WorkFlow’s custom code,the following error was occurred.

 ![_0-1697077356258.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/f/f9484b90de7419c86a838a2842a5acc3577711e5.png)

 ![_1-1697077389830.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/3/5/3595e06c655e7ea58834b36f7fefc53f11edff59.png)

---

<div class="post-metadata">

**Author:** ![zachh](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/zachh/32/87065_2.png) [@zachh](https://community.hubspot.com/u/zachh)\
**Post date:** [October 18, 2023, 1:38pm UTC](https://community.hubspot.com/t/associations-cant-be-created-by-custom-code-in-workflow/91558/5 "2023-10-18T13:38:46Z")

</div>

Hi [@張志明](https://community.hubspot.com/t5/user/viewprofilepage/user-id/641822)

I’m looking at [the Associations API documentation](https://developers.hubspot.com/docs/api/crm/associations) and the code looks different from what you have.

Your association API code says:

const apiResponse = await hubspotClient.crm.associations.v4.batchApi. **create**

But the API documentation for Create endpoint shows:

const apiResponse = await hubspotClient.crm.associations.v4.batchApi. **post**

Your Custom Object endpoint code also uses CREATE instead of POST. [The endpoint in the documentation](https://developers.hubspot.com/docs/api/crm/crm-custom-objects) looks more like this:

`const apiResponse = await hubspotClient.crm.objects.basicApi.post-/crm/v3/objects/{objectType}(objectType, SimplePublicObjectInputForCreate);`

Can you try these edits and see if it changes anything?

- Zach
