function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ManojKumar MuthuManojKumar Muthu 

System.QueryException: value of filter criterion for field 'CreatedDate' must be of type dateTime

Hi there,
I got stuck here, out of 13 filter criteria I have achive 11 and rest I am stuggling to sorting out,

Below my code, here I am trying to achive is, 
In the Priority field, which is single pick list field where I have pass more than one value
For Example: Priority: 1 & 2
Secondly in the CreatedDate field I have to pass a date range
For Example: Createddate= 2018-12-12 to 201-1-1

Apex Class,
@RestResource(urlMapping='/QueryFilterStatus/*')
global with sharing class QueryFilterStatus{
    
    @HttpPost
    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 +'\'';
    
    if(String.isNotBlank(Owneremail))
    {
       str+=' AND OwneEmail__c=\''+ Owneremail+'\'';
    }
    if(String.isNotBlank(Bugis))
    {
       str+=' AND BugIs__c=\''+ Bugis+'\'';
    }
    if(String.isNotBlank(Version))
    {
       str+=' AND Version__c=\''+ Version+'\'';
    }
    
    if(String.isNotBlank(tracker))
    {
       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+'\'';
    }
    
    if(String.isNotBlank(CStatus))
    {
       str+=' AND CStatus__c=\''+ CStatus+'\'';
    }
    if(String.isNotBlank(Product))
    {
       str+=' AND Product_Type__c=\''+ Product+'\'';
    }
    if(String.isNotBlank(Name))
    {
       str+=' AND ContactName__c Like \'%'+ Name+'%\'';
    }
    if(String.isNotBlank(OwnerName))
    {
       str+=' AND Case_Owner_Name__c Like \'%'+ OwnerName+'%\'';
    }
    
    if(String.isNotBlank(priority))
    {
    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;
}
}
 
Can someone help with this,
TIA
Raj VakatiRaj Vakati
Change your code like this

https://eprasu.wordpress.com/2010/01/25/salesforce-converting-datetime-to-format-yyyy-mm-ddthhmmssz/

​​​​​​​
if(String.isNotBlank(CreateDate))
    {

	
       str+=' AND CreatedDate = \''+ DateTime.valueOf(CreateDate).format('yyyy-MM-dd\’T\’hh:mm:ss\’z\')+'\'';
    }

 
ManojKumar MuthuManojKumar Muthu

@raj
Thanks for the reply,
I just sligtly modify and it got saved without any error as below,

if(String.isNotBlank(createdate))
    {

    
       str+=' AND CreatedDate = :DateTime.valueOf(createdate).format(YYYY-MM-DDThh:mm:ss+hh:mm)';
    } 

But When I run the code, I am getting this error,
System.QueryException: unexpected token: ( Class.QueryFilterStatus.dopostCase: line 89, column 1)
 
At this line;  caseList=Database.query(str);
    return caseList;

 

Raj VakatiRaj Vakati
Can you print the query and see what values is coming ??