You need to sign in to do that
Don't have an account?
Kelvin 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!
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!
Try this
String Name='pre\\*post';
List<List<SObject>> searchList = [FIND :Name IN ALL FIELDS RETURNING Lead(Id, Name)];
Regrds,
MCS
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'.
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);
Can you check again.
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.
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
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