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
Shilpa KambleShilpa Kamble 

Dynamic SOSL with hyphen in search string

Does anyone know How to search the string with hyphen(-) in dynamic SOSL?
 
string searchStr = 'EG-0006201';
string str = 'FIND  \''+searchStr+'*\'IN ALL FIELDS REturning Event_Registration__c (Id,Name)';
system.debug('str='+str);

List<List <sObject>> Rslt= search.query(str );
system.debug('Rslt='+Rslt);

returning 0 results.
 
Shilpa KambleShilpa Kamble
Hi Piyush,

Yes, I have record in salesforce database.
This query does't gove any error in salesforce apex, but if I try this in developer console I get following error
mismatched character '-' expecting '}
there is problem with '-' hyphen symbol in search string.
How do I so that?

 
sfdcMonkey.comsfdcMonkey.com
in developer console query editor try this query
FIND {EG\-0006201*} IN ALL FIELDS REturning Event_Registration__c (Id,Name)
let me inform if it helps you
thanks

 
sfdcMonkey.comsfdcMonkey.com
The following characters are reserved:
[? & | ! { } [ ] ( ) ^ ~ * : \ " ' + -]

Reserved characters, if specified in a text search, must be escaped (preceded by the backslash \ character) in order to be properly interpreted. An error occurs if you do not precede reserved characters with a backslash. This is true even if the SearchQuery is enclosed in double quotes.
For example, to search for the following text:
 
{1+1}:2

insert a backslash before each reserved character:
\{1\+1\}\:2
i hope it helps you
thanks
 
Shubham Bangad 12Shubham Bangad 12
Thanks a lot Piyush Soni. It really helped me out.