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);
}
}