In order to write a where clause against a datetime field, you need to format your dateTime object into the following format:
WHERE SystemModstamp > 2005-10-08T01:02:03Z
How do you correctly convert the dateTime object to a String?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this community Datetime dt = Datetime.now();
String formattedDt = dt.formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
Update: Another option is to use SOQL dynamic binding, no formatting required.
Datetime dt = Datetime.now();
String query = 'SELECT Id FROM User WHERE SystemModstamp > :dt '
List<SObject> results = Database.query(query);
JSON.serialize(dt).replace('"', '')
can solve it. * with the second method, :dt
must be in the same scope with Database.query(soql)
Just use datetime variable in the SOQL where clause.
For example:
Datetime dt = DateTime.newInstance(2005,10,8,01,02,03);
List<Account> accounts = [SELECT Id, Name FROM Account WHERE CreatedDate < :dt];
System.debug(accounts.size());