You need to sign in to do that
Don't have an account?

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
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
Put debug after second line for checking whether query1 returns correct soql or not.
System.debug('query1 :::'+query1);
Thanks,
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
Modify the query :
String query1 = 'SELECT Id FROM '+ Object1+ ' WHERE LastModifiedDate < : '+ custom.Date__c;
String query = 'SELECT Id FROM '+ Object1+ ' WHERE LastModifiedDate <: '+ clean1.Date__c;
No error.
But the batch is not working as expected :(
with Regards,
Nisha
System.debug('Query::'+Database.getQueryLocator(query));
13:11:52:057 FATAL_ERROR System.QueryException: unexpected token: '2017-05-26'
the above error is still there.
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);
datetime LastRunTime = custom.Date__c;
String strQuery ='SELECT Id FROM '+ Object1+ ' WHERE LastModifiedDate <: LastRunTime';
return Database.getQueryLocator(strQuery);
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
Is this SOQL returning any records? Please check by putting the debug statement.
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.
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);
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.
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());