APIs & Integrations

DZoladz1
Participant

Workflow custom code: Create association to custom object

SOLVE

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

1 Accepted solution
webdew
Solution
Guide | Diamond Partner
Guide | Diamond Partner

Workflow custom code: Create association to custom object

SOLVE

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=',
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.

View solution in original post

0 Upvotes
2 Replies 2
webdew
Solution
Guide | Diamond Partner
Guide | Diamond Partner

Workflow custom code: Create association to custom object

SOLVE

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=',
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.

0 Upvotes
DZoladz1
Participant

Workflow custom code: Create association to custom object

SOLVE

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...

0 Upvotes