Look up contact by phone

I’d like to look up contacts by phone number over the API when an email address is not available.

I’ve tried both DataService.query and DataService.findByField with no luck.

Taking a known phone number in from the Phone1 field in an existing contact record (ie (555) 555-5555), I am able to look up records by that number in the web interface. I am not able to find records using the same number over the API with ‘query’ or ‘findByField’. I tested the following values against Phone1.

555
5555555555
(555) 555-5555
%5%

Is there anything special about querying a phone field?

I tried sending just ‘Phone’ as the field name and got an appropriate error. For good measure, I tried this process with multiple phone numbers.

At this point I feel like there must be something really obvious I am missing.

If it matters, I’m working with the Novak PHP SDK. Sample code:

private function lookupInfusionsoftContactByPhone($phone) {
    return $this->lookupInfusionsoftRecord('Phone1', $phone);
}

private function lookupInfusionsoftRecord($key, $value) {
    return \Infusionsoft_DataService::findByField(
        new \Infusionsoft_Contact(),
        $key, $value, null, null, false, $this->infusionsoft );
}

Thanks!

I found my error. In findByField, I was sending ‘null’ for the number of records to return. Ask and you will receive!

And for reference, the record matched on the formatted phone number and on the wildcard:

Matches:
(555) 555-5555
%5%

Does not match:
555
5555555555

I’m not really sure though if I can trust that Infusionsoft will always store phone numbers in this format. Maybe possibly for Phone1, Phone2, etc.

So just in case I am throwing in a bunch of wildcards, ie

%5%5%5%5%5%5%5%5%5%5%

private function formatPhoneForMatching($phone) {
    $numbers = str_split(preg_replace('/\D/', '', $phone));
    return '%' . implode('%', $numbers) . '%';
}

@calebtr,

You cannot trust that the format of the phone number field will be consistent. There are too many variations. This, as I’m sure you are seeing, creates a difficulty in your case as the query is a string literal query. If the phone numbers were pre-filterable then that would be different. The only way to be sure in your case is to remove all non-number characters and then compare, which you can’t do until the information is pulled from IS. I might consider a small script that takes the phone number, strips all non numerics and then stored the number only value in a custom field. Then you would have a more reliable query when you needed it.

Good idea. And since the wildcard support for dataservice.query is undocumented, I probably shouldn’t rely on it.

Hey,

The way you mentioned above works just fine and it really helped me a lot for searching contacts on IFS with phone numbers, irrespective of the format they are saved in.

Thanks :raised_hands:

1 Like