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
Afzaal HassanAfzaal Hassan 

Database query syntax error

Hello
I have written a scheduled apex batch job which compiles fine. However, when I ran the job, the queue (in the UI) says the following error:
"First error: value of filter criterion for field 'Client_Released_Date__c' must be of type dateTime and should not be enclosed in quotes". Below is my start method for the scheduled batch apex:

   global Database.QueryLocator start(Database.BatchableContext BC) {
        Datetime clientReleasedDate = Datetime.now().addHours(-1);
        return Database.getQueryLocator(
            'SELECT Id, Client_Released__c, Client_Number__c, Client_Release_Date__c, ' +
            'Email_Sent__c FROM Account ' + 
            'WHERE Email_Sent__c = true AND Client_Released__c = true AND Client_Release_Date__c>= \''+ clientReleasedDate +'\''     
        );

    }

I check the client_released_date__c and I confirmed that it is a datetime field. So the datatype matches. I am suspecting I have my SOQL query syntax wrong. Perhaps I am escaping the quotes when I dont need to. Can someone please check and correct my method/syntax for the query locator?
Thanks
Raj VakatiRaj Vakati
Try this 

 
global Database.QueryLocator start(Database.BatchableContext BC) {
      
	
	  DateTime clientReleasedDate = System.now().addHours(-1);
       
        return Database.getQueryLocator([
            SELECT Id, Client_Released__c, Client_Number__c, Client_Release_Date__c, 
            Email_Sent__c FROM Account 
            WHERE Email_Sent__c = true AND Client_Released__c = true AND Client_Release_Date__c>=:clientReleasedDate ]   
        );

    }

 
Raj VakatiRaj Vakati
Or you can do it as below
 
global Database.QueryLocator start(Database.BatchableContext BC) {
        Datetime clientReleasedDate = Datetime.now().addHours(-1);
		
    String formattedDt = clientReleasedDate.format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\'');
        return Database.getQueryLocator(
            'SELECT Id, Client_Released__c, Client_Number__c, Client_Release_Date__c, ' +
            'Email_Sent__c FROM Account ' + 
            'WHERE Email_Sent__c = true AND Client_Released__c = true AND Client_Release_Date__c>=:formattedDt'     
        );

    }