How to Associate CRM Objects Automatically with HubSpot Custom-Coded Workflows

A practical guide to associating HubSpot CRM objects automatically using custom-coded workflow actions and the Associations API, with code you can adapt.

John Kelleher
John Kelleher

HubSpot's standard automation can associate records when you tell it which two records to link. It struggles when the link has to be worked out at runtime: associate the company on this deal with the custom object on the same deal, for every deal, without anyone touching it. That is where custom-coded workflow actions in Operations Hub earn their place. You write a small piece of JavaScript that calls the CRM Associations API and creates the link for you.

This guide walks through the pattern we use to associate objects automatically: take a deal, find the company attached to it, find the custom object attached to it, and associate those two together. It is genuinely useful if you are comfortable in HubSpot and want to build it yourself. If you would rather have your CRM architecture and data automation built to hold up under load, that is what we do at SpotDev, and the link to talk to us is at the end.

What you need before you start

  • Operations Hub Professional or Enterprise. Custom-coded workflow actions only exist on these tiers.
  • The right workflow object type. This example uses a deal-based workflow, which needs Sales Hub Professional or Enterprise.
  • A private app access token with the scopes for the objects you are reading and writing, set as a workflow secret so it is not hard-coded.
  • The association type ID between your two objects. You can find this in the CRM Development settings, or pull it from the associations schema API. You need it to tell HubSpot which kind of relationship to create.

The goal and acceptance criteria

Be precise about the outcome before you write a line of code. For this pattern the criteria are simple: when a deal is enrolled, the company associated with that deal becomes associated with the custom object associated with that deal, using the correct association type, and the action fails cleanly with a log entry if either record is missing. Defining the failure case up front is the difference between an automation you can trust and one that silently leaves gaps in your data.

Step 1: Identify the deal

Custom-coded actions receive data through input fields you define in the workflow editor. Pass the deal's Record ID in as an input named dealId, then read it at the top of your action.

  • Add an input field in the action UI, map it to the enrolled deal's Record ID.
  • Read it in code: const dealId = event.inputFields['dealId'];

Working from the Record ID rather than a property value keeps the action reliable. Record IDs do not change, so the same code behaves the same way on every enrolment.

Step 2: Find the associated company

The custom-coded action gives you a pre-authenticated hubspotClient, so you can call the CRM APIs directly without standing up any external middleware. Use the Associations API to read which company is linked to the deal.

  • Call the associations read endpoint for the deal-to-company relationship, passing the dealId.
  • Take the first result's id as your company Record ID.
  • Guard for the empty case. If a deal has no company, stop and log it rather than letting the action throw.

Step 3: Find the associated custom object

Repeat the same read against the deal-to-custom-object association. The shape of the call is identical, you just change the target object type to your custom object's type ID. You now hold both Record IDs you need: the company and the custom object.

  • Read the deal-to-custom-object associations for the same dealId.
  • Capture the custom object's Record ID from the response.
  • Again, handle the case where nothing is associated, so a partial deal does not break the run.

Step 4: Associate the two objects

With both IDs in hand, create the link. The associations batch endpoint is the right tool even for a single pair, because it takes the explicit association type ID and is forgiving if the link already exists. Send the company Record ID as the from object, the custom object Record ID as the to object, and your association type ID as the relationship.

Because the batch endpoint is idempotent for an existing association, you can re-enrol records safely without creating duplicates or errors. That matters when you backfill historic deals through the same workflow.

How the full action fits together

The complete action follows a clean, predictable order, and that order is the point. Each step has one job and a guard clause, so when something goes wrong you know exactly which step failed.

  1. Read dealId from the input fields.
  2. Read the deal's associated company, exit early if none.
  3. Read the deal's associated custom object, exit early if none.
  4. Call the associations batch endpoint to link company and custom object.
  5. Return outputs or log a clear message on any missing data.

Wrap the body in a try/catch, log the IDs you resolved at each step, and return a status output the workflow can branch on. Those few extra lines turn a black box into something your operations team can actually debug six months from now.

Where this pattern breaks, and what to do about it

The tutorial version works on a clean deal with exactly one company and one custom object. Real CRMs are messier. Deals with multiple companies, custom objects that should resolve to a primary record, rate limits when you backfill thousands of records at once, and association labels that need to be set as well as the association itself, all turn a tidy example into a project. Custom-coded actions also have an execution time limit, so heavy logic belongs in a serverless function or a proper integration rather than a workflow step.

This is the line between a useful script and a robust CRM architecture. Getting the object model right, choosing where automation should live, and making sure associations stay consistent as data flows in from other systems is the work that keeps a mid-market CRM trustworthy as you scale. It is exactly the kind of build we deliver through our HubSpot CRM implementation and data engineering services. If the associations need to be driven by data living in another platform, our custom HubSpot integrations connect those systems so the right records link automatically at the source.

Build it once, build it right

Custom-coded workflows are a powerful way to make HubSpot do what off-the-shelf automation cannot. They are also code, which means they deserve the same care as any other part of your stack: clear acceptance criteria, sensible error handling, and a plan for scale. Get those right and your CRM associations stay clean without manual intervention.

If you would rather have the architecture and the automation built to hold up, talk to us about your HubSpot CRM build.

Talk to us about your HubSpot CRM build

John Kelleher

John Kelleher

Author
John is the founder and the Chief Executive at SpotDev.