[SalesForce] Escape Sequence Single Quotes Error

I am creating a dynamic query.

string s = '%' + searchText + '%';
string query = 'Select name, ShippingCity, ShippingState, ShippingCountry, rating, type, Industry, ownerId From Account Where name LIKE : '+s+' OR BillingAddress LIKE : '+s+' OR website LIKE : '+s+' OR AccountNumber LIKE : '+s;

System.QueryException: line 1:122 no viable alternative at character '%'

Best Answer

You need to wrap it in single quotes, not precede it with a colon (:).

String s = '\'%' + String.escapeSingleQuotes(searchText)  + '%\'';
... 'LIKE ' + s + '...';

Or if you want to use bind variables, you can just wrap your searchText variable:

searchText = '%' + searchText + '%';
... 'LIKE :searchText ...';
Related Topic