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

Hi,

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>{'Brain-Prick@gmail.com','Dorsy.Young@coe.com', 'nj-sf@dev.com'};
	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
            if(name.indexof('-')>=0){
                name = name.replace('-', '\\-');
            }
            nameList = nameList + name + ' OR ';
        }
        
        nameList = nameList.substringBeforeLast('OR').trim();
        
        return nameList;
    }

Thanks,
N.J
 
darkwaterdarkwater
Try this 

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

Thanks

 
Shilpa KambleShilpa Kamble
Hey Nilesh,

Did you get the answer?