I am building a dynamic SOSL Query
searchText = 'AB-C';
searchquery = 'FIND {'+ searchText + '} IN all FIELDS RETURNING Account( id, name, ownerid,website, CMFL__Oppty_Count__c WHERE '+ condition +' order by createddate DESC )';
System.debug(searchquery);
Debug
FIND {AB-C*} IN all FIELDS RETURNING Account( id, name,Additional_domains__c, ownerid,website WHERE (Website_Domain__c='ab\-c.com' OR Additional_domains__c like '%ab\-c.com%') order by createddate DESC )
When the query is run it returns an error
System.QueryException: line 1:8 mismatched character '-' expecting '}'
I thought i havent escaped the string for special chars and hence this error. I removed the '-' in AB-C and now i get this error on the where condition where i have escaped in the where conditions
Invalid string literal 'ab\-c.com'. Illegal character sequence '\-' in string literal.
When should i escape the string and when i should not while creating a dynamic query