HubSpot / hubspot-api-ruby

HubSpot API Ruby Client Libraries for V3 version of the API
Apache License 2.0
114 stars 57 forks source link

How do associations work? #330

Open eprislac opened 2 months ago

eprislac commented 2 months ago

Please document this. I've been trying for two days to get all child companies for a given parent company, I can see the data in Hubspot, I know the website at least can do it. How does one do it with this gem?

max-pi commented 1 month ago

Hi @eprislac , did you end up figuring this out? I'm trying to add associations after creating company and user, but HUBSPOT_CLIENT.crm.companies.associations_api.create doesn't seem to be a valid method path. Unclear where associations_api is if anywhere..

bschrag620 commented 3 weeks ago

object_type and to_object_type => ['contact', 'company', 'deal'] for example object_id and to_object_id => the relative hubspot ids

hubspot_client.crm.associations.v4.basic_api.create(
        object_type:,
        object_id:,
        to_object_type:,
        to_object_id:,
        association_spec:
      )

association_spec is a bit trickier. The full list can be found here.

I use the following mapping to grab the correct spec_id:

  HUBSPOT_DEFINED_ASSOCIATION_IDS = {
    deal: {
      contact: 3,
      company: 341,
      deal: 451
    },
    contact: {
      contact: 449,
      company: 279,
      deal: 4
    },
    company: {
      contact: 280,
      company: 450,
      deal: 342
    }
  }.freeze

spec_id = HUBSPOT_DEFINED_ASSOCIATION_IDS.dig(object_type, to_object_type)