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
user23232323user23232323 

System.now in Dynamic SOQL

Hi,

I could use some assistance with an apex error Im receiving from a batch process. Im trying to filter my batch using System.now() but am getting an unexpected token: "(' error from the getQueryLocator line. How do I get System.now() to translate properly so the query will execute? Thanks ahead of time.
 
global class BatchTempName implements Database.Batchable<sObject>, Database.Stateful {

	String query = '';
	
	global BatchTempName() {
		query = 'SELECT Name,Custom_Date_Time__c, Unavailable__c FROM User where Custom_Date_Time__c <: System.now() and Unavailable__c = true';
	}

	global Database.QueryLocator start(Database.BatchableContext BC) {
		return Database.getQueryLocator(query);
	}

    ...
	
}



 
Best Answer chosen by user23232323
Raj VakatiRaj Vakati
Try like this

Refer this link 

https://salesforce.stackexchange.com/questions/100187/how-to-compare-datetime-field-with-systemtime-now-in-soql-query

 
global class BatchTempName implements Database.Batchable<sObject>, Database.Stateful {

	String query = '';
	DateTime currentTime = System.now();

	global BatchTempName() {
		query = 'SELECT Name,Custom_Date_Time__c, Unavailable__c FROM User where Custom_Date_Time__c <: currentTime and Unavailable__c = true';
	}

	global Database.QueryLocator start(Database.BatchableContext BC) {
		return Database.getQueryLocator(query);
	}

    ...
	
}

 

All Answers

Raj VakatiRaj Vakati
Try like this

Refer this link 

https://salesforce.stackexchange.com/questions/100187/how-to-compare-datetime-field-with-systemtime-now-in-soql-query

 
global class BatchTempName implements Database.Batchable<sObject>, Database.Stateful {

	String query = '';
	DateTime currentTime = System.now();

	global BatchTempName() {
		query = 'SELECT Name,Custom_Date_Time__c, Unavailable__c FROM User where Custom_Date_Time__c <: currentTime and Unavailable__c = true';
	}

	global Database.QueryLocator start(Database.BatchableContext BC) {
		return Database.getQueryLocator(query);
	}

    ...
	
}

 
This was selected as the best answer
Ramesh DRamesh D
try this
global class BatchTempName implements Database.Batchable<sObject>, Database.Stateful {

	String query = '';
	
	global BatchTempName() {
		query = 'SELECT Name,Custom_Date_Time__c, Unavailable__c FROM User where Custom_Date_Time__c <: '+System.now()+' and Unavailable__c = true';
	}

	global Database.QueryLocator start(Database.BatchableContext BC) {
		return Database.getQueryLocator(query);
	}

    ...
	
}

 
Vsevolod Tsurikov 4Vsevolod Tsurikov 4
My version of building the query to support dateTime format 
String query = ''SELECT Name,Custom_Date_Time__c, Unavailable__c FROM User where Custom_Date_Time__c < '+Datetime.now().format('yyyy-MM-dd\'T\'HH:mm:ss.SSSXXX')+' and Unavailable__c = true';

Please note, it is not '<:', just '<'