Hi @dennisedson
Havn’t got the current code any quicker. Started to change tact and looked at storing a local copy of the data in the site and using cron to sync it. Not ideal from a maintenace POV.
Your summary sounds correct. It was done (in this cumbersome way) to get around the limitation of HS not allowing contacts to be in multiple companies. I see that is in beta but I’m not sure those associations will allow for “association specific properties” though.
Your step 4 is correct. It was this bit: “merge the contact properities with the deal ones”.
The posted settings sent to the deals endpoint is this:
$post = [
'filterGroups' => [[
'filters' => [
[
'propertyName' => 'associations.company',
'operator' => 'EQ',
'value' => $companyID
],
[
'propertyName' => 'pipeline',
'operator' => 'EQ',
'value' => '2495083'
],
[
'propertyName' => 'closedate',
'operator' => 'NOT_HAS_PROPERTY'
]
]
]],
'properties' => [
'practice_fte_deal_',
'employment_start_date',
'employment_left_date',
'firstname',
'dealname'
],
'limit' => '100'
];
$deals = $this->cURL('POST', 'https://api.hubapi.com/crm/v3/objects/deals/search', $post);
That’s the quick part around 0.4s.
Those deal results are looped and this is the code for getting the contact out of the deal:
$post = [
'filterGroups' => [[
'filters' => [
[
'propertyName' => 'associations.deal',
'operator' => 'EQ',
'value' => '' # set inside the loop
]
]
]],
'properties' => [
'firstname',
'lastname',
'type_of_employee',
# fields removed for brevity
],
'limit' => '100',
'sorts' => [[
'propertyName' => 'firstname',
'direction' => 'ASCENDING'
]]
];
foreach ($deals->results as $deal)
{
$post['filterGroups'][0]['filters'][0]['value'] = $deal->id;
$contacts = $this->cURL('POST', 'https://api.hubapi.com/crm/v3/objects/contacts/search', $post);
if (isset($contacts->results))
{
foreach ($contacts->results as $contact)
{
$fields = [];
# Contact fields
$fields['HPI'] = $contact->properties->hpi_number;
# fields removed for brevity
# Deal fields
$fields['FTE'] = $deal->properties->practice_fte_deal_;
# fields removed for brevity
}
$staff[$fields['HubSpotID']] = $fields;
}
}
return $staff;
By the end of this, the data is all the contacts (deal and contact properties merged) that have a deal in a given company.
There is 4926 contacts in total in this HS instance. I’m not sure where the latency come in. Just the overhead of the API round trip, some kind of applied rate limit to many requests close together or is searching the contacts by a deal id not efficient.
Putting some simple timing around the calls - they look like this:
Deals took 0.53 seconds.
This contact took 0.47 seconds.
This contact took 0.48 seconds.
This contact took 0.54 seconds.
This contact took 0.51 seconds.
This contact took 0.47 seconds.
This contact took 0.56 seconds.
This contact took 0.61 seconds.
This contact took 0.62 seconds.
This contact took 0.51 seconds.
This contact took 0.62 seconds.
This contact took 0.52 seconds.
This contact took 0.61 seconds.
This contact took 0.41 seconds.
This contact took 0.72 seconds.
This contact took 0.46 seconds.
This contact took 0.57 seconds.
This contact took 0.46 seconds.
This contact took 0.47 seconds.
This contact took 0.51 seconds.
This contact took 0.51 seconds.
This contact took 0.51 seconds.
This contact took 0.51 seconds.
This contact took 0.42 seconds.
This contact took 0.50 seconds.
This contact took 0.49 seconds.
Whole thing took 13.58 seconds.