associations can't be created by custom code in workflow

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

 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>

Hi @張志明,

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

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

dear @zachh

thanks very much for your reply. :slightly_smiling_face:

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]

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?

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);
}

@zachh

When i use associations API-V4(Accounts Dashboard | HubSpot) 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.

Hi @張志明

I’m looking at the Associations API documentation 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 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