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
Nilesh Jagtap (NJ)Nilesh Jagtap (NJ) 

Dynamic SOSL with hyphen in search string


I am trying to perform dynamic search with sosl but gettig 0 rows.
For same query it is returning 4 rows on workbench.
here is the code snippet for refernce:
list<String>lstNames = new list<String>{'','', ''};
	String searchquery='FIND {'+ getNames(lstNames) +'} IN ALL FIELDS RETURNING Account(Id,Name),Contact(Id,Name), Lead(Id, Name)'; 
	public string getNames(list<string> lstNames){
        String nameList ='';
        for(String name:lstNames){
            //escape hyphen
                name = name.replace('-', '\\-');
            nameList = nameList + name + ' OR ';
        nameList = nameList.substringBeforeLast('OR').trim();
        return nameList;

Try this 

Change name = name.replace('-', '\\-'); to name = name.split('-')[0]+'*';


Shilpa KambleShilpa Kamble
Hey Nilesh,

Did you get the answer?