+ Start a Discussion
someonesomeone 

SOSL search for phone number in e164 format

I have some contacts that have their phone numbers stored in e164 format (e.g. +18885551234). If I execute the below search with a phone param of 8885551234, the query returns nothing. Why is that?
 
String queryStr = 'FIND {*' + ph + '} IN Phone FIELDS RETURNING Contact(Phone, FirstName, LastName)';

 
ShotShot
ph is a String object?
someonesomeone
Correct
ShotShot
You cant use * at the begining of the word, you can use it in the middle on in the end of the word
someonesomeone
Hmm ok. So I guess a simple work around would be to change the FIND to have a + at the beginning?
String queryStr = 'FIND {\\+*' + ph + '} IN Phone FIELDS RETURNING Contact(Phone, FirstName, LastName)';

 
ShotShot
FIND {\+*' + ph + '} IN Phone FIELDS RETURNING Contact(Phone, FirstName, LastName)
One slash is enough.
 
someonesomeone
One slash give me a compile error: 
Error: Compile Error: Invalid string literal 'FIND {\+*'. Illegal character sequence '\+' in string literal.

And actually, come to think of it, this will only work if the all the numbers in SF are prepended with a +
ShotShot
Yes, you are right, two slash need to add , i checked it in query tab dev console, this one is working fine, i thought it will be fine for you too :
FIND {\+*885551234} IN Phone FIELDS RETURNING Contact(Phone, FirstName, LastName)
ShotShot
This one should work perfectly:
String ph = '885551234';
String q = 'FIND {\\+*' + ph + '} IN Phone FIELDS RETURNING Contact(Phone, FirstName, LastName)';
List<List <sObject>> c  = search.query(q);
Contact result = (Contact)c[0][0];
System.debug('Phone ' + result.Phone);
Result:
DEBUG|Phone +18885551234
 
someonesomeone
What if the number in salesforce is not prefixed with '+' ? What happens if the number is stored as 8885551234, then that query will not return anything, correct?
ShotShot
yep!