⚙ Operations Hub

Hollyce
Contributor | Diamond Partner
Contributor | Diamond Partner

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!

1 Accepted solution
CMcKay
Solution
Top Contributor

Add an association label when associating Contact to Deal with custom-coded workflow actions

SOLVE
On this line:
hubspotClient.crm.deals.associationsApi.create(searchDealResponse.body.results[0].id,'contacts', event.object.objectId,'deal_to_contact');
Swap deal_to_contact with the association type id of your choice

View solution in original post

0 Upvotes
4 Replies 4
KevinWilliams
Participant | Elite Partner
Participant | Elite Partner

Add an association label when associating Contact to Deal with custom-coded workflow actions

SOLVE

What end point were you using here as the ones I see in the docs only allow for HUBSPOT_DEFINED and "label" doesnt seem to do anything for this guy https://api.hubapi.com/crm-associations/v1/associations/create-batch. My thanks in advance.

 

0 Upvotes
CMcKay
Solution
Top Contributor

Add an association label when associating Contact to Deal with custom-coded workflow actions

SOLVE
On this line:
hubspotClient.crm.deals.associationsApi.create(searchDealResponse.body.results[0].id,'contacts', event.object.objectId,'deal_to_contact');
Swap deal_to_contact with the association type id of your choice
0 Upvotes
Hollyce
Contributor | Diamond Partner
Contributor | Diamond Partner

Add an association label when associating Contact to Deal with custom-coded workflow actions

SOLVE

Thanks so much! For reference for other users, I used the actual label of the association id ("policyholder") instead of the ID number. 

FNeri
Participant | Elite Partner
Participant | Elite Partner

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

{
  "category": "USER_DEFINED",
  "typeId": 7,
  "label": "Influencer"
}