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
hermihermi 

Date field on a query string

HI,

I am trying to develop a batch class where i need to query the value of a date field, but i am getting the following error. Please help.

 

date newdate=system.today()+6;
 
//String targetDate = targetDateval.format('yyyy-MM-dd')+'T'+targetDateval.format('HH:mm:ss')+'Z'; 
system.debug(' Date'+newdate);
query= 'Select recordtypeid,ispersonaccount,id,ownerid,Date_to_Call__c from Account';
query+= ' Where Date_to_Call__c ='+'\''+newdate+'\'' +' or Date_to_Call__c = today   ';
Error:  value of filter criterion for field 'Date_to_Call__c' must be of type date and should not be enclosed in quotes
if i dont enclose it in quotes and if i just give the query as following, i am getting other error
query= 'Select recordtypeid,ispersonaccount,id,ownerid,Date_to_Call__c from Account';
 query+= ' Where Date_to_Call__c ='+newdate+' or Date_to_Call__c = today   ';
error: line 1:277 no viable alternative at character ' '
Please suggest any ideas..
  

 

hermihermi

Thanks for the reply Mike...I tried changing the format but its still giving the same error. Can you post some sample code if you have any. I think usage of escape characters for Date field is different.

 

Thanks in Advance

Imran MohammedImran Mohammed

Did you try the following?

query+= ' Where Date_to_Call__c = :newDate or Date_to_Call__c = today   ';

_Prasu__Prasu_

Try this for formating the date: 

DateTime.now().format(‘yyyy-MM-dd\’T\’hh:mm:ss\’z\”)