Add an association label when associating Contact to Deal with custom-coded workflow actions
SOLVE
I have a working custom-coded workflow action that finds a Deal with a property value matching the value of the Contact's property. It succesfully associates the Deal with the Contact. I would like to assign an association label as well, but I can't quite get the syntax right.
Here is the successful code:
// Import the Hubspot NodeJS Client Library - this will allow us to use the HubSpot APIs
const hubspot = require('@hubspot/api-client');
/*
This function is called when the custom code action is executed. It takes 2 arguements. The first is the event object which contains information on the currently enrolled object.
The second is the callback function which is used to pass data back to the workflow.
*/
exports.main = (event, callback) => {
// Instantiate a new HubSpot private app (secret)
const hubspotClient = new hubspot.Client({
accessToken: process.env.custom_code_private_app
});
// Retrive the currently enrolled contacts "Policy ID" property
hubspotClient.crm.contacts.basicApi.getById(event.object.objectId, ["policy_id"])
.then(results => {
// Get data from the results and store in variables
let policyIdValue = results.body.properties.policy_id;
//console.log("SEARCH TERM: " + policyIdValue); // - FOR DEBUG
// Create search criteria
const filter = { propertyName: 'policy_id', operator: 'EQ', value: policyIdValue }
const filterGroup = { filters: [filter] }
const sort = JSON.stringify({ propertyName: 'policy_id', direction: 'DESCENDING'})
const properties = ['policy_id']
const limit = 1
const after = 0
const searchCriteria = {
filterGroups: [filterGroup],
sorts: [sort],
properties,
limit,
after
}
// Search the CRM for Deals matching "policyID" variable defined earlier
hubspotClient.crm.deals.searchApi.doSearch(searchCriteria).then(searchDealResponse => {
//console.log("RESULTS: " + searchDealResponse.body.total); // - FOR DEBUG
// If total equals 0 no results found
if(searchDealResponse.body.total == 0){ console.log("DEAL WITH Policy ID " + policyIdValue + "NOT FOUND.") // - FOR DEBUG
}else{ // MATCH FOUND - ASSOCIATE DEAL TO CONTACT
// console.log("DEAL " + Policy ID + " FOUND: ASSOCIATE RECORDS"); // - FOR DEBUG
//Associate Deal with Contact
hubspotClient.crm.deals.associationsApi.create(searchDealResponse.body.results[0].id,'contacts', event.object.objectId,'deal_to_contact');
}
});
callback({outputFields: {}});
})
.catch(err => {
console.error(err);
});
}
I know I need to use these variables somehow:
{
associationCategory: 'USER_DEFINED',
associationTypeId: 2
}
But the examples on the Associations v4 documentation page did not give me the right syntax for custom-coded workflow actions. Can anyone help? Thanks in advance!
Add an association label when associating Contact to Deal with custom-coded workflow actions
SOLVE
THANK YOU SO MUCH!
I had the SAME EXACT scenario, literally!! For others that stumble this way, USE THE LABEL OF THE ASSOCIATION TYPE
Below is a custom association set in our account.
Using the LABELVALUE worked (Influencer), despite Hub's code snippet saying to use something completly different. I'd love for Hubspot to fix their code snippet because I'd like to know officially what to use for future instances so this doesn't break down the road.