Hey!
My end goal is to get a list of companies that have (any) completed tasks within a given time period. This turned out to be trickier than I expected.
I’d want to avoid looping single companies and taks, because the amount of calls may be very high. I have tried several approaches but all seem to stop to the limitations in search/batch APIs.
Approach 1
1. Search tasks that have been completed within a given timeframe. Return associated company IDs.
2. Batch read associated companies by returned company IDs.
Problem:
Associated company IDs cannot be retrieved from Search Tasks endpoint. Apparently I would have to check associations per task, which I wouldn’t want to do.
Approach #2:
1. Search tasks that have been completed within a given timeframe.
2. Use Associations API to batch read all associated companies by returned task IDs. Return company IDs.
3. Batch read associated companies by returned company IDs.
Problem:
I haven’t found a way to batch read associations from tasks to companies. Is there a way to do it?
Or is there a completely different approach that would take me to my goal, without looping through objects?
Thanks for your help!
##### EDIT 1 #####
Was able to find an endpoint that returns a list of associations between given Task IDs and companies. However, I could find a way to do it by using V4 API, like documented here. An example for V4 would be golden.
I used the Python library hubspot-api-client that has the
hubspot.crm.associations.batch_api.read() method for which you can pass a list of IDs as an input.
from hubspot import HubSpot
from hubspot.crm.associations import BatchInputPublicObjectId
params = {
'from_object_type': 'tasks',
'to_object_type': 'companies',
'batch_input_public_object_id': BatchInputPublicObjectId(
inputs=[{"id": 1, "id": 2}]
)
}
hs = HubSpot() # Use your access token
hs.crm.associations.batch_api.read(**params)
##### EDIT 2 #####
Next problem: I cannot get a list of companies with the returned company IDs by using Company Read Batch endpoint. It’s probably because company ID is not a unique property (it has has_unique_value as False). Why on earth is it so?
This starts to seem like a huge overkill for such a simple task. I can’t be the only one who wants to get a list of tasks, with the associated companies…
##### EDIT 3 #####
Okay, got the thing done by using Company Search endpoint, searching by company IDs. Would’ve thought that batch API was created for something like “get companies by IDs”, but I guess I was wrong.
So eventually this turned out to be a wish to return associated company IDs (if requested), when searching for Tasks.