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
Nisha Babu 8Nisha Babu 8 

How to compare two datetime in apex

Hi,

I have to compare the LastModifiedDate with the custom setting date.

Custom_Setting__c custom = Custom_Setting__c .getValues(Object1);
String query1 = 'SELECT Id FROM '+ Object1+ ' WHERE LastModifiedDate < :'+ custom.Date__c;
CustomSetting date is in dateTime.

The query is not showing any error ,but it is not giving the expected result :(
Anyone could help me on that?

With Regards,
    Nisha
Dhanya NDhanya N
Hi Nisha,
Put debug after second line for checking whether query1 returns correct soql or not. 
System.debug('query1 ​:::'+query1);

Thanks,
Dhanya
Nisha Babu 8Nisha Babu 8
Hi Dhanya,

The query returned is:
12:40:21:011 USER_DEBUG [110]|DEBUG|SELECT Id FROM Account WHERE LastModifiedDate <:2017-05-26 10:25:00


The error I got in batch class
12:44:15:100 EXCEPTION_THROWN [34]|System.QueryException: unexpected token: '2017-05-26'

With Regards,
 Nisha
Dhanya NDhanya N
After <: there should be space.
Modify the query : 
String query1 = 'SELECT Id FROM '+ Object1+ ' WHERE LastModifiedDate < : '+ custom.Date__c;
Nisha Babu 8Nisha Babu 8
Hi Dhanya,

String query = 'SELECT Id FROM '+ Object1+ ' WHERE LastModifiedDate <: '+ clean1.Date__c;

No error.
But the batch is not working as expected :(

with Regards,
    Nisha
 
Dhanya NDhanya N
Check if the query returns record in debug logs.
System.debug('Query::'+Database.getQueryLocator(query));
Nisha Babu 8Nisha Babu 8
Dhanya,
13:11:52:057 FATAL_ERROR System.QueryException: unexpected token: '2017-05-26'
the above error is still there.
Pankaj_GanwaniPankaj_Ganwani
Hi Nisha,

Just use below mentioned code:

Custom_Setting__c custom = Custom_Setting__c .getValues(Object1);
DateTime dtTimeValue = custom.Date__c;
String query1 = 'SELECT Id FROM '+ Object1+ ' WHERE LastModifiedDate < dtTimeValue';
Database.getQueryLocator(query1);
Dhanya NDhanya N
Store custom.Date__c in dateTime variable and use that variable inside query. Check if that works.

datetime LastRunTime = custom.Date__c;
String strQuery ='SELECT Id FROM '+ Object1+ ' WHERE LastModifiedDate <: LastRunTime';
return Database.getQueryLocator(strQuery);

 
Nisha Babu 8Nisha Babu 8
Hi Dhanya and Pankaj,

14:22:22:013 USER_DEBUG [111]|DEBUG|SELECT Id FROM Budget__c WHERE LastModifiedDate <: 2017-05-26 10:25:00
This is the query in batchclass

But it is not deleting records :(

With Regards,
    Nisha
Pankaj_GanwaniPankaj_Ganwani
Hi Nisha,

Is this SOQL returning any records? Please check by putting the debug statement.
Nisha Babu 8Nisha Babu 8
Hi Pankaj

15:05:26:016 USER_DEBUG [22]|DEBUG|Query in batch:SELECT Id FROM Budget__c WHERE LastModifiedDate < :2017-05-26 10:25:00
this is in the query available in batch constructor.

inside the start() method ,the system.debug() is not showing in the debug log.

 
Pankaj_GanwaniPankaj_Ganwani
Hi Nisha,

Can you execute this query from developer console and see for the results? i.e.

Custom_Setting__c custom = Custom_Setting__c .getValues(Object1);
DateTime dtTimeValue = custom.Date__c;
String query1 = 'SELECT Id FROM '+ Object1+ ' WHERE LastModifiedDate <: dtTimeValue';
Database.Query(query1);
Nisha Babu 8Nisha Babu 8
Hi Pankaj,

In batch class I have to pass the query :(
In the execute anonymous it is working, but in batch it is showing error like 15:47:11:051 FATAL_ERROR System.QueryException: Variable does not exist: dtTimeValue.
I don't want to pass the date to the batch class.
Pankaj_GanwaniPankaj_Ganwani
Okay, Got it. In your start method, can you try to do something like this?

Custom_Setting__c custom = Custom_Setting__c .getValues(Object1);
SELECT Id FROM '+ Object1+ ' WHERE LastModifiedDate <:'+DateTime.newInstance(custom.Date__c.year(), custom.Date__c.month(),custom.Date__c.day(), custom.Date__c.hour(),custom.Date__c.minute(),custom.Date__c.second());