I am generating a dynamic SOQL
but when I run my SOQL
I am getting an error 1:213 no viable alternative at character ' '
My code
/*
* admin approval panal
*/
public class ctrl_admin_approval_panal{
public List<Approval__c> apprLst {get; set;} // To get the soql result
public Approval__c appr {get; set;} // getting the value form Vf page
Transient String query ;
public string startDate;
public string endDate;
public ctrl_admin_approval_panal(){
appr = new Approval__c ();
apprLst = new List<Approval__c>();
}
// Search button
public PageReference searchBtn(){
System.debug('Start Date before '+ appr.Search_Data__c);
System.debug('End date before ' + appr.End_Date__c);
query = 'SELECT Name, Approval_Status__c, IP_Address_Save__c, Offer_Name__c, Offer_Name__r.Name FROM Approval__c WHERE Approval_Status__c = False';
if(appr.User__c != null){
query = query + ' and User__c = \'' + appr.User__c + '\'' ;
if(appr.IP_Address_Save__c != null){
query = query + ' and IP_Address_Save__c = \'' + appr.IP_Address_Save__c + '\'' ;
}
if(appr.Search_Data__c != null){
query = query + ' and Work_Done_on__c < ' + appr.Search_Data__c;
query = query + ' and Work_Done_on__c > ' + appr.End_Date__c;
}
}else{
if(appr.IP_Address_Save__c != null){
query = query + ' and IP_Address_Save__c = \'' + appr.IP_Address_Save__c + '\'' ;
}
if(appr.Search_Data__c != null){
query = query + ' and Work_Done_on__c < ' + appr.Search_Data__c;
query = query + ' and Work_Done_on__c > ' + appr.End_Date__c;
}
}
system.debug('Query result ' + query );
apprLst = Database.query(query );
system.debug('Approval List ' + apprLst );
return null;
}
}
When I try my SOQL
in WorkBench
I also get an error:
MALFORMED_QUERY:
Work_Done_on__c < 2013-12-29T15:34:10 and Work_Done_on__c > 2014-02-01T15:34:10
^
ERROR at Row:1:Column:193
line 1:193 no viable alternative at character ' '
My SOQL
statement:
SELECT Name, Approval_Status__c, IP_Address_Save__c, Offer_Name__c FROM Approval__c WHERE Approval_Status__c = False and User__c = 'a009000000' and Work_Done_on__c < 2013-12-29T15:34:10 and Work_Done_on__c > 2014-02-01T15:34:10
When I remove the Work_Done_on__c
field the SOQL
works properly. Does anybody have a solution to this problem?