Below my code, I want to pass date value as a parameter but I facing an error as below, message: System.QueryException: value of filter criterion for field 'CreatedDate' must be of type dateTime and should not be enclosed in quotes Class.QueryFilterStatus.dopostCase: line 85, column 1

@RestResource(urlMapping='/QueryFilterStatus/*') global with sharing class QueryFilterStatus{

global static List<Case> dopostCase(String CreateDate,String Owneremail, String Bugis, String Version, String accountid,String tracker,String priority,String contactEmail,String status,String CStatus,String subject, String Keyword, String Product, String Name, String OwnerName) {
List<Case> caseList = new List<Case>();
String str='Select id, CreatedDate, Case_Owner_Name__c, OwneEmail__c, Description, Subject, Case_Type__c, Code__c, Tracker__c, status, Version__c, type, priority, ContactEmail, ContactId, OwnerId, CaseNumber, CSM_ID__c, CUP_Phone__c, Call_Type__c, IsClosed, Product_Type__c, ContactName__c, GS_Account__c from Case where accountid =\''+ accountid +'\'';

   str+=' AND OwneEmail__c=\''+ Owneremail+'\'';
   str+=' AND BugIs__c=\''+ Bugis+'\'';
   str+=' AND Version__c=\''+ Version+'\'';

   str+=' AND Tracker__c =\''+ tracker+'\'';
// if(String.isNotBlank(priority))

// { // str+=' AND Priority=\''+ priority+'\''; // } if(String.isNotBlank(contactEmail)) { str+=' AND ContactEmail=\''+ contactEmail+'\''; } if(String.isNotBlank(status)) { str+=' AND Status =\''+ status+'\''; }

   str+=' AND CStatus__c=\''+ CStatus+'\'';
   str+=' AND Product_Type__c=\''+ Product+'\'';
   str+=' AND ContactName__c Like \'%'+ Name+'%\'';
   str+=' AND Case_Owner_Name__c Like \'%'+ OwnerName+'%\'';

for(string pstring : priority.split(',')){
   str+=' AND Priority=\''+ pstring +'\'';

} if(String.isNotBlank(CreateDate)) { str+=' AND CreatedDate = \''+ CreateDate+'\''; } if(String.isNotBlank(Keyword)) { str+=' AND (Subject Like\'%'+ Keyword+'%\''; str+=' OR CaseNumber Like \'%'+ Keyword+'%\')'; }
System.debug(str); caseList=Database.query(str); return caseList;

} }


You are trying to filter your records on CreatedDate field which is of DateTime type. Couple of things you should keep in mind when filtering records by DateTime field are

  • The format used for filtering on DateTime field should be YYYY-MM-DDThh:mm:ssZ
  • It should not be enclosed in quotes

So here are the changes you need to do

if(String.isNotBlank(CreateDate)) { 
    //Convert your CreateDate to the format mentioned above using the DateTime functions or 
    //using your custom method  and assign it to DateTime variable dt
    //(It depends on which format you are getting the createDate)
    //DateTime dt = convert your string to DateTime
    str+=' AND CreatedDate =:dt'; //updated code
  • Yeah Vijay I tried the above code, I am geting this error, Illegal string literal: Line breaks are not allowed in string literals at line 73 column 10 – Manojkumar Muthu Feb 13 '19 at 6:41
  • Can you post SOQL which is getting generated i.e. post the output of this line System.debug(str); – Vijay Ganji Feb 13 '19 at 6:47
  • @VijayGanji You have a typo in your answer. Please double-check. – sfdcfox Feb 13 '19 at 6:49
  • @VijayGanji Just updated the below line str+=' AND CreatedDate =:dt' and now I can able to save, When I run, { "accountid":"0010l00000Vry45", "Owneremail":" ", "Bugis":" ", "Version":" ", "tracker":" ", "priority":" ", "contactEmail":" ", "status":" ", "CStatus":"Open", "Product":" ", "Name":" ", "OwnerName":" ", "dt":"2013-10-05 20:03:20" } Error: System.QueryException: Invalid bind expression type of String for column of type Datetime Class.QueryFilterStatus.dopostCase: line 82, column 1 – Manojkumar Muthu Feb 13 '19 at 6:54
  • @ManojkumarMuthu What is the datatype of dt, you must have used String, it should be DateTime – Vijay Ganji Feb 13 '19 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.