Hi,
see custom code listed here. The goal is to set an association between company and deal based on a property in the deal. This property is always filled.
// Get the HubSpot client
const hubspot = require(‘@hubspot/api-client’);
const http = require(“https”);
const {execSync} = require(‘child_process’);
// Call main function
exports.main = (event, callback) => {
return callback(processEvent(event));};
// Process the work
function processEvent(event) {
// HAPIKEY is defined in secrets at the Workflow / Custom Code
// Instantiate the HS Client
const hubspotClient = new hubspot.Client({
accessToken: process.env.WorkflowCustomCode
});
// Interval
const intMin = 2;
const intMax = 4;
// Get the Deal ID
const dealId = event.object.objectId;
// Declare constants
const fromObjectType = “Deal”;
const toObjectType = “companies”;
const associationType = “deal_to_company”;
// Declare variables
let companyId = 0
// Get the my_store_id property value
// It looks like we need to use the searchApi instead of the basicApi as the custom properties are not returned by the basicApi
const dealProperties = [“order_store_gln”]
const dealFilter = { propertyName: “hs_object_id”, operator: “EQ”, value: dealId }
let dealFilterGroup = { filters: [dealFilter] }
let dealSearchRequest = { properties: dealProperties, filterGroups: [dealFilterGroup] }
execSync('sleep ’ + randomIntFromInterval(intMin, intMax));
hubspotClient.crm.deals.searchApi.doSearch(dealSearchRequest)
.then (dealResults => {
if (dealResults.body.total > 0) {
// We have found the deal
// Let’s get the company based on order_store_gln
let storeId = dealResults.body.results[0].properties.order_store_gln;
if (storeId > 0) {
const companyFilter = { propertyName: “storegln”, operator: “EQ”, value: storeId }
const companyFilterGroup = { filters: [companyFilter] }
const companySearchRequest = { filterGroups: [companyFilterGroup] }
execSync('sleep ’ + randomIntFromInterval(intMin, intMax));
hubspotClient.crm.companies.searchApi.doSearch(companySearchRequest)
.then (companyResult => {
if (companyResult.body.total > 0) {
companyId = companyResult.body.results[0].id
if (companyId > 0) {
// We have found the company to associate with
// Let’s check if there already is an association with another company; that one should be deleted.
const BatchInputPublicObjectId = { inputs: [{“id”:dealId}] };
try {
execSync('sleep ’ + randomIntFromInterval(intMin, intMax));
hubspotClient.crm.associations.batchApi.read(fromObjectType, toObjectType, BatchInputPublicObjectId)
.then (assResults => {
if (assResults.body) {
for (let i = 0; i < assResults.body.results.length; i++) {
let assCompanyId = assResults.body.results[i].to[0].id
if (assCompanyId != companyId) {
// Delete the current association
var options = {
“method”: “DELETE”,
“hostname”: “api.hubapi.com”,
“port”: null,
“path”: “/crm/v4/objects/Deal/” + dealId + “/associations/Company/” + assCompanyId + “?hapikey=” + process.env.HAPIKEY,
“headers”: {
“accept”: “application/json”
}
};
execSync('sleep ’ + randomIntFromInterval(intMin, intMax));
var req = http.request(options);
req.end();
}
}
}
})
// We can now create the new association
try {
execSync('sleep ’ + randomIntFromInterval(intMin, intMax));
hubspotClient.crm.deals.associationsApi.create(dealId, toObjectType, companyId, associationType)
} catch (e) {
e.message === ‘HTTP request failed’
? console.error(JSON.stringify(e.response, null, 2))
: console.error(e)
}
} catch (e) {
e.message === ‘HTTP request failed’
? console.error(JSON.stringify(e.response, null, 2))
: console.error(e)
}
}
}
})
}
}
})
}
function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}