2

I do have a company created in HubSpot and the code below works with the exception that no matter what I pass to setValue() it always returns the entire list of all companies on file instead only the one existing i.e. hubspot.com

tried it also with setPropertyName('hubspot') - same result.

Any ideas?

BTW. I asked the HubSpot community already but no one replied yet.

public function searchCompany($company_domain)
{

    $filter = new \HubSpot\Client\Crm\Companies\Model\Filter();
    $filter->setOperator('EQ')->setPropertyName('domain')->setValue($company_domain);

    $searchRequest = new \HubSpot\Client\Crm\Companies\Model\PublicObjectSearchRequest();
    $searchRequest->setFilterGroups([$filter]);

    try {
        $response = $this->hubspot_client->crm()->companies()->searchApi()->doSearch($searchRequest);
        return $response;
    } catch (ApiException $e) {
         return $e->getMessage();
    }

}

1 Answers1

3

to return the company's ID using HubSpot's searchAPI() method, I had to pass the filters into the filterGroups method FIRST instead of passing only the filters to the PublicSearchRequest()->setFilterGroups() ... obviously ...

so, the correct method look now like this:

    public function searchCompany($company_domain)
{
    $filter = new \HubSpot\Client\Crm\Companies\Model\Filter();
    $filter->setPropertyName('domain');
    $filter->setOperator('EQ');
    $filter->setValue($company_domain);

    $filterGroup = new \HubSpot\Client\Crm\Companies\Model\FilterGroup();
    $filterGroup->setFilters([$filter]);

    $searchRequest = new \HubSpot\Client\Crm\Companies\Model\PublicObjectSearchRequest();
    $searchRequest->setFilterGroups([$filterGroup]);

    try {
        $response = $this->hubspot_client->crm()->companies()->searchApi()->doSearch($searchRequest);
        return $response['results'][0]['id'];
    } catch (ApiException $e) {
         return "Exception when calling search_api->do_search: " . $e->getMessage();
    }
}

and now the response is returning the correct result instead of all companies.