For those interested: Search by Custom Property (Workaround)


#1

Hi Hubspot Community,

Like some of you I was frustrated that I was unable to perform a company/contact search with the only thing available being query on Contacts which only searched a few fields.

I am currently in the progress of building a bridge API to enable such things and yesterday I finished the contact search. This code I have written could also be applied to other types of record.

The PHP code snippet from my workaround (this may not work for millions of contacts but tested upto 15,000, may work for more tho dependant on server setup):

The class snippet =======================================================================

function fetchContacts($propertySearch = array(), $attributes = array())
{
    $contactResults = array();
    $vidOffset = 0;
    $hasMore = 0;
    $hapiKey = "YOUR_HAPIKEY";
    $hasMore = true;

    $properties = implode("&property=", $attributes);

    while($hasMore == true) {
        if(count($attributes) != 0) {
            $contactsURL = "https://api.hubapi.com/contacts/v1/lists/all/contacts/all?hapikey=$hapiKey&count=2000&vidOffset=$vidOffset&property=$properties";
        } else {
            $contactsURL = "https://api.hubapi.com/contacts/v1/lists/all/contacts/all?hapikey=$hapiKey&count=2000&vidOffset=$vidOffset&property=$properties";
        }
        $resultSet = json_decode(file_get_contents($contactsURL), true);

        $hasMore = $resultSet["has-more"];
        $vidOffset = $resultSet["vid-offset"];

        /*
         * Put results into a readable array() for searching.
         */
        foreach ($resultSet["contacts"] as $contact) {
            $contactArray = array();
            $contactArray["vid"] = $contact["vid"];
            foreach ($contact["properties"] as $key => $value) {
                $contactArray[$key] = $value["value"];
            }

            array_push($contactResults, $contactArray);
        }


    }

    $contactFilteredResults = array();

    foreach($propertySearch as $propkey => $propvalue) {
        for($i=0; $i < count($contactResults); $i++) {
            if(array_key_exists($propkey, $contactResults[$i])) {
                if ($contactResults[$i][$propkey] == $propvalue) {
                    array_push($contactFilteredResults, $contactResults[$i]);
                }
            }
        }
    }

    if(count($propertySearch) != 0) {
        var_dump(count($contactFilteredResults));
        return $contactFilteredResults;
    } else {
        var_dump(count($contactResults));
        return $contactResults;
    }

}

Calling Function Demo ===================================================

$properties =  array(
"firstname",
"lastname",
"company",
"sync_to_netsuite",
"send_sample_kit",
"phone",
"address",
"city",
"state",
"zip",
"country",
"associatedcompanyid",
"email"); //These are the fields to get

$parameters = array("sync_to_netsuite" => true); //This is what to search for,

print_r(fetchContacts($parameters, $properties));

Result Snippet =========================================================

Array ( [0] => Array ( [vid] => 987365 [zip] => NR44 TR1 [firstname] => Bobby [address] => Fungi Road [city] => Kiddiminster[lastmodifieddate] => 1542006113907 [company] => Ding Dong Groceries Ltd [email] => imanemail@gmail.com [lastname] => Baggings) );

=====================================================================

NOTE: to get all contacts just call fetchContacts();

Its by no means perfect but may help some of you for the time being to perform searches.


#3

This is AWESOME! Thank you so much for sharing your work with the community!


#4

No problem, I believe in sharing ideas. :slight_smile: hopefully this functionality will be officially added in future api updates :+1: