I’m trying to buid a workflow with custom code that detects when a certain company property has changed and then gives me the individial contacts associted with that company. I’m really struggling with the correct path for the API call, and am unsure which of the three below are the correct route:
Hi @James_Swallow ,
please use this code for custom code workflow
const hubspot = require(‘@hubspot/api-client’);
const request = require(“request”);
exports.main = async (event, callback) => {
const hubspotClient = new hubspot.Client({
// apiKey: process.env.HAPIKEY
apiKey: ‘apikey’
});
let hubspot_owner_id;
try {
const ApiResponse = await hubspotClient.crm.contacts.basicApi.getById(event.object.objectId, [“hubspot_owner_id”]);
hubspot_owner_id = ApiResponse.body.properties.hubspot_owner_id;
var options = { method: ‘POST’,
url: ‘https://api.hubapi.com/crm/v3/objects/company/search?hapikey=apikey’,
headers:
{
‘Content-Type’: ‘application/json’ },
body: {
“limit”: 100,
“properties”: [ “pipeline” ],
“filters”:[
{
“propertyName”: “”,
“operator”: “”,
“value”: “”
},
{
“propertyName”: “associations.contact”,
“operator”: “EQ”,
“value”: event.object.objectId
}
]
},
json: true };
request(options, function (error, response, body) {
if (error) throw new Error(error);
if(body.total == 0)
// No
callback({
outputFields: {
dataStatus: body
}
});
else
// Yes
});
} catch (err) {
console.error(err);
// We will automatically retry when the code fails because of a rate limiting error from the HubSpot API.
throw err;
}
// this is for testing
// we need only hubspot_owner_id
callback({
outputFields: {
hubspot_owner_id: hubspot_owner_id,
}
});
}
Hope this helps!
If we were able to answer your query, kindly help the community by marking it as a solution.
Thanks and Regard.