function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kelvin CheungKelvin Cheung 

Escaping wildcard characters in SOSL

I've had some trouble with SOSL in escaping wildcard characters (* and ?).  From the SOSL documentation (http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_sosl_find.htm), it seems like * should be escaped with \* or possibly \\* or even \\\\*, but none of those seem to work.

These are the results I saw:
[FIND 'pre*post' IN ALL FIELDS RETURNING Lead(Id, Name)];  // works as a wildcard
[FIND 'pre\*post' IN ALL FIELDS RETURNING Lead(Id, Name)];  // error: Invalid string literal 'pre\*post'. Illegal character sequence '\*' in string literal.
[FIND 'pre\\*post' IN ALL FIELDS RETURNING Lead(Id, Name)];  // error: Invalid string literal 'pre\*post'. Illegal character sequence '\*' in string literal.
[FIND 'pre\\\\*post' IN ALL FIELDS RETURNING Lead(Id, Name)]; // seems to function as wildcard same as no backslash

Similar behavior is observed with ? instead of *.

Any help?  Thanks in advance!
ManjunathManjunath
Hi ,

Try this

String Name='pre\\*post';
List<List<SObject>> searchList = [FIND :Name IN ALL FIELDS RETURNING Lead(Id, Name)];

Regrds,
MCS

 
Kelvin CheungKelvin Cheung

Manjunath,

Thanks for the reply. I'm surprised that has different behavior, though it does. Still, it doesn't find a name 'pre*post', but it does find 'pre\*post'.
ManjunathManjunath
Kelvin,

I tried it in console


String Name='pre\\*post';
List<List<SObject>> searchList = [FIND :Name IN ALL FIELDS RETURNING Lead(Id, Name)];
System.debug(searchList);


User-added image

Can you check again.
 
Kelvin CheungKelvin Cheung
That's odd.  Here's what I'm doing in console:
String str = 'pre\\*post';
List<List<SObject>> searchList = [FIND :str IN ALL FIELDS RETURNING Lead(Id,Company)];
System.debug(searchList);

It's finding records like 'pre\QWERTYpost', which to me indicates the backslash is being escaped but the asterisk is serving as a wildcard.

User-added image
ManjunathManjunath
Kelvin,

I believie it is escaping the "\" ( String str = 'pre\\*post';)  not the "*".
I am also getting the same result, As I dint have any record in the system initally.

I will let you know if i find any alternative.

Regards,
MCS

 
William YehWilliam Yeh
Looks like this issue is related to known issue going on for SOSL 
These has been already escalated to R&D and pending for Fix.

Here are the Known issue articles :
https://success.salesforce.com/issues_view?id=a1p30000000T4fKAAS 
https://success.salesforce.com/issues_view?id=a1p300000008XdRAAU