Flexible Deal Association with Lead Records

Status: Idea Submitted

Currently, the only way you can associate a deal to a lead record is by using the default lead stage setting that automatically creates a deal upon lead stage = qualified.

However, if you turn off that lead stage setting, and the user then creates a deal manually after changing the lead stage to “qualified”, the Lead record is not able to be associated to the Deal.

This creates a gap in our reporting and results in a number of complicated workflows to make this connection work. The ideal future state would give us flexibility on how to set the rules for when and how a lead record can be associated with a deal. Creating a deal automatically upon a lead stage change is nice but it doesn’t align with our internal lead processes.

We need this too. Also you can’t associate activity directly with a Lead, making it complex where we have multiple live Leads against a specifc contact.

Same here. We have two different teams. One that creates deals from the leads…and one that just places orders (that get loaded in after the fact as a lead). As such we can’t turn on the preference that prompts them to create a deal from a lead (since there is no way to decline to create a deal). Would LOVE a way for them to assocate a deal with a lead after the fact, or at least allow me to set the prefefernce at the user/team level. Or…have more than one lead pipeline so that we can set the preference there.

This is a huge gap in reporting! If there is a lead on a contact with an associated deal it should automatically associate the lead to the deal. Doesn’t matter how it started.

Please fix this HubSpot! We have a situation where we want to use a workflow to create the Deal from a qualified Lead (to be able to shovel/copy some data around in the backend to help the Sales rep) and we have literally no way of automatically associating that Deal with the Lead - it’s insane that this isn’t accessible to us since it is with practically every other object.

Agree with this. Sales reps aren’t always perfect at following process…

Hi! I made a fix for the ones that are using Operations Hub.
I firstly do want to say, this should be default behavior. The back-end setup to do this via the API, is exactly the same as any other object.

Here we go!
What you’ll first need to have, is of course your workflow, that is Lead based, and will create a Deal object via the “Create Record” action.

After this, create a Custom Coded action. And do the following:

  • Add your secret key (Make sure your Private app has the “crm.objects.leads.write” scope), (I called mine ACCESS_TOKEN)
  • Add the “hs_created_object” Id property. (Click on “Add Property” > “Select Proeprty” > Click on enrolled lead, change to “Action Outputs” > Click on “The created object” )
  • Add the Record ID of the lead (Similar as above, just search for “Record ID” under properties for “Enrolled Lead”)
  • Add the code at the bottom of this message in the code block
  • All done!

I do want to note a couple of things. This code is not properly set for error debugging etc. It’s using the basic HubSpot documentation provided code, which shouldn’t be prone to change that much. Please read throught he code, I noted comments with // so it should be easy to understand for anyone, but never just plug & play any piece of code into your own system unless you are sure what it does.

In short words, it takes the 2 ids, prepares an API call to associate them, and associates the 2 objects.

This should be default, and it definitely shouldn’t require a user to have Operations Hub. Please fix this internally HubSpot before more users start using this workarround and doing unnececary API calls that will become outdated.

The code:

// Load in the Hubspot API library.
const hubspot = require('@hubspot/api-client');

exports.main = async (event, callback) => {

 // The ID for the newly created deal.
 const hs_created_object = event.inputFields['hs_created_object'];

 // The ID for the currently enrolled Lead.
 const hs_object_id = event.inputFields['hs_object_id'];

 // Load & Authenticate the Hubspot Client.
 const hubspotClient = new hubspot.Client({"accessToken":process.env.ACCESS_TOKEN});

 // Define the association variables.
 // 582 is the default Leads > Deals ID.
 const objectType = "leads";
 const objectId = hs_object_id;
 const toObjectType = "deals";
 const toObjectId = hs_created_object;
 const AssociationSpec = [
 {
 "associationCategory": "HUBSPOT_DEFINED",
 "associationTypeId": 582
 }
 ];

 // Do the API call to associate.
 try {
 const apiResponse = await hubspotClient.crm.associations.v4.basicApi.create(objectType, objectId, toObjectType, toObjectId, AssociationSpec);
 console.log(JSON.stringify(apiResponse, null, 2));
 } catch (e) {
 e.message === 'HTTP request failed'
 ? console.error(JSON.stringify(e.response, null, 2))
 : console.error(e)

 // Throw for when the API reaches a limit error.
 throw(e)
 }
}

I actually made a mistake int he above, and as I can not edit the above, I am cloning my reply and changing it.

Main difference are these:

  • The Create ID is not “clean” and needs to be cleaned up (Remove the portal ID and assocaition IDs)
  • I changed to the Default assocaitions API, as the one with Association ID didn’t work
  • I added a constant in the code for portal ID. This is different for everyone, so needs to be changed in the code!

Hi! I made a fix for the ones that are using Operations Hub.
I firstly do want to say, this should be default behavior. The back-end setup to do this via the API, is exactly the same as any other object.

Here we go!
What you’ll first need to have, is of course your workflow, that is Lead based, and will create a Deal object via the “Create Record” action.

After this, create a Custom Coded action. And do the following:

  • Add your secret key (Make sure your Private app has the “crm.objects.leads.write” scope), (I called mine ACCESS_TOKEN)
  • Add the “hs_created_object” Id property. (Click on “Add Property” > “Select Proeprty” > Click on enrolled lead, change to “Action Outputs” > Click on “The created object” )
  • Add the Record ID of the lead (Similar as above, just search for “Record ID” under properties for “Enrolled Lead”)
  • Add the code at the bottom of this message in the code block
  • Change your portal ID in the code! This can be found in the url of any Hubspot page. Example contacts page: https://app-eu1.hubspot.com/contacts/PORTALID
  • All done!

I do want to note a couple of things. This code is not properly set for error debugging etc. It’s using the basic HubSpot documentation provided code, which shouldn’t be prone to change that much. Please read throught he code, I noted comments with // so it should be easy to understand for anyone, but never just plug & play any piece of code into your own system unless you are sure what it does.

In short words, it takes the 2 ids, cleans up the created record ID, prepares an API call to associate them, and associates the 2 objects.

This should be default, and it definitely shouldn’t require a user to have Operations Hub. Please fix this internally HubSpot before more users start using this workarround and doing unnececary API calls that will become outdated.

The code:

// Load in the Hubspot API library.
const hubspot = require('@hubspot/api-client');
const portalId = "xxxxxxxxx"; // Replace with Your portal id! This can be found in the url of any Hubspot page. Example contacts page: https://app-eu1.hubspot.com/contacts/PORTALID

exports.main = async (event, callback) => {

 // The ID for the newly created deal.
 const prefix = portalId + "-0-3-";
 let hs_created_object = event.inputFields['hs_created_object'];
 hs_created_object = hs_created_object.slice(prefix.length);

 // The ID for the currently enrolled Lead.
 const hs_object_id = event.inputFields['hs_object_id'];

 // Load & Authenticate the Hubspot Client.
 const hubspotClient = new hubspot.Client({"accessToken":process.env.ACCESS_TOKEN});

 // Define the association variables.
 const fromObjectType = "leads";
 const fromObjectId = hs_object_id;
 const toObjectType = "deals";
 const toObjectId = hs_created_object;

 // Do the API call to associate.
 try {
 const apiResponse = await hubspotClient.crm.associations.v4.basicApi.createDefault(fromObjectType, fromObjectId, toObjectType, toObjectId);
 console.log(JSON.stringify(apiResponse, null, 2));
 } catch (e) {
 e.message === 'HTTP request failed'
 ? console.error(JSON.stringify(e.response, null, 2))
 : console.error(e)
 throw (e);
 }
}

@Arno-VdW Would it be possible for you to show your workflow here? I’m confused as to what the triggers would be and how the deal record gets created. Specifically does the workflow run on: All New Leads? All New Leads that have been set to Qualified? Not all leads would we want a deal created for, etc. Just would be helpful to see how the workflow is built (very much appreciate you posting the custom code module by the way!)
@hubspot PLEASE build in functionality to associate a lead with a deal (when created from the lead record)
Thanks in advance!

I completely understand your case. It’s not the most optimal solution, but if you’re an admin, a possible “simple workaround” would be to move the lead stage back to a previous stage and then forward again to “Qualified” or “Open deal”, and at that point associate the specific deal.

Actually about a year ago, when creating a Deal from a Lead-based workflow, I could associate new deal and enroll Lead. But recently when HubSpot updated Lead beta, this completely disappeared.

Yes please. We also have a use case where a deal comes in automatically created via a Zapier integration, and we need to backtrack creating the lead record and associating that lead record to the deal. There’s currently no solution for this given the current features.

Hi HubSpot,

We are stumbling upon the same problem. Could you please implement a proper association?

Hi team, commenting for a customer here - as they would also appreciate having this feature. They are having deals created by API, and would like to be able to associate it to leads so that they can be moved to the qualified stage automatically via workflow. Thanks!

We would also like this please. commentiing on behalf of some of our customers.

I am actually not sure if the custom code is still needed / if this is in a closed beta (I can’t find it in any of the lead betas), but I seem to be able to:

- Create a Lead
- Qualify that Lead
- Create a Deal in the workflow

The source of the deal is the workflow, and the lead is associated after this as well, I think it is because of the following setting:

I don’t think this is part of any Beta. The workflow for this is just a trigger, and the create deal workflow action.