# Workflow custom code: Create association to custom object

**URL:** <https://community.hubspot.com/t/workflow-custom-code-create-association-to-custom-object/49363>\
**Category:** APIs & Integrations\
**Tags:** companies, developer-tools, professional\
**Created:** [January 6, 2022, 10:01pm UTC](https://community.hubspot.com/t/workflow-custom-code-create-association-to-custom-object/49363 "2022-01-06T22:01:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DZoladz1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dzoladz1/32/2490_2.png) [@DZoladz1](https://community.hubspot.com/u/DZoladz1)\
**Post date:** [January 6, 2022, 10:01pm UTC](https://community.hubspot.com/t/workflow-custom-code-create-association-to-custom-object/49363/1 "2022-01-06T22:01:34Z")

</div>

Hello. My company just purchased Ops hub pro and I’m taking advantage of the custom code actions in the workflows to create an association between a Company and a custom object. Unfortunately I’m not to advanced at JS as you can see from the code below. I’m attempting to pull in two properties from the Company object (Company ID (hs\_object\_id( and a custom Company property (china\_company\_id) to use in the code. When I test the code it threw some undefined errors, ":“Runtime.UserCodeSyntaxError”. I assume its from the variables for those two properties I’m bring in.

Any feedback would be helpful. Thanks in advance.

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

const hubspotClient = new hubspot.Client({  
apiKey: process.env.HAPIKEY  
});

const companyId = event.inputFields[hs\_object\_id];  
const toObjectType = 2-3852312; //custom object id  
const toObjectId = event.inputFields[china\_company\_id];  
const associationType = 20; //association id

try {  
const apiResponse = await hubspotClient.crm.companies.associationsApi.create(companyId, toObjectType, toObjectId, associationType);  
console.log(JSON.stringify(apiResponse.body, null, 2));  
} catch (e) {  
e.message === ‘HTTP request failed’  
? console.error(JSON.stringify(e.response, null, 2))  
: console.error(e)  
}

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [January 7, 2022, 8:24am UTC](https://community.hubspot.com/t/workflow-custom-code-create-association-to-custom-object/49363/2 "2022-01-07T08:24:29Z")

</div>

Hi @DZoladz1 ,  
Please use this code: var options3 = { method: ‘POST’,  
url: ‘[https://api.hubapi.com/crm/v3/associations/p9383513\_lead\_activity/contact/batch/create?portalId=](https://api.hubapi.com/crm/v3/associations/p9383513_lead_activity/contact/batch/create?portalId=)’,  
qs: { hapikey: ‘’ },  
headers:  
{  
‘Content-Type’: ‘application/json’ },  
body: {  
“inputs”: [  
{  
“from”: {  
“id”: body1.id  
},  
“to”: {  
“id”: event.object.objectId  
},  
“type”: “lead\_activity\_to\_contact”  
}  
]  
},  
json: true };  
request(options3, function (error3, response3, body3) {  
if (error3) throw new Error(error3);  
console.log(body3);  
});  
Hope this helps!  
If we were able to answer your query, kindly help the community by marking it as a solution.  
Thanks and Regard.

---

<div class="post-metadata">

**Author:** ![DZoladz1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dzoladz1/32/2490_2.png) [@DZoladz1](https://community.hubspot.com/u/DZoladz1)\
**Post date:** [January 7, 2022, 9:14pm UTC](https://community.hubspot.com/t/workflow-custom-code-create-association-to-custom-object/49363/3 "2022-01-07T21:14:39Z")

</div>

Thank you very much for the feedback, I really appreciate it. So if I understand the code correctly when it runs it will associate all pairs of objects identified in the request body, which in the code above is lead activites and contacts, correct? If so, I have the association between Company and the Custom Object already established. What I’m trying to do is associate a specific Company (hs\_object\_id) in the Company object with its sibling Company in the Custom Object. Sorry if I’m mudding up the water. Thank you again…
