'Getting System.QueryException: line 1:40 no viable alternative at character '\'

Am using a dynamic query to get data. Below is my code

if(objVal != null){
      sObject obj = Schema.getGlobalDescribe().get(objVal).newSObject();
      if(ConditionVal== null || ConditionVal=='')
     query = 'Select Id From '+objVal+ ' Limit 10000';
        else{
           ConditionVal= String.escapeSingleQuotes(ConditionVal);
           query = 'Select Id From '+selectedObj+' WHERE '+ConditionVal+' LIMIT 10000';
        }      
        List<SObject> objlst = Database.query(query);

Am getting "objVal" , "ConditionVal" value form VF page
Example objVal = 'Account' ConditionVal = 'industry = 'Apparel'
after executing the code am getting the query like this

Select Id From Account WHERE industry = \'Apparel\' LIMIT 10000


Solution 1:[1]

update the value of ConditionVal variable as below,

ConditionVal = 'Industry = \'Apparel\'';

the query will work as expected.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vikas Naik