You need to sign in to do that
Don't have an account?
need to compare date field in query string
Hi,
Here I am doing some crazy stuff. I get givenDateTime from controller and capturing it to a dattime variable using constructor of BatchApex. I want to use this dattime variable in quey. But when I use 'dtGMT' variable in quey i got some conversion errors. so, I converted GMT time(default sFDC object store GMT time) to MST time. then its working fine. But now i thought I did crazy stuff for date.. Do any work around for this without converting from GMT to MST.
Appreciate any suggestions. Following is the sample code I created.
global BatchMemberList (ID CampId, dateTime givenDateTime)
{
CampaignId=CampId;
dtGMT= givenDateTime;
}
global Database.Querylocator start(Database.BatchableContext BC)
{
//converting GMT to MST and getting individaul values from the datetime
String dtMST = dtGMT.format('yyyy-MM-dd HH:mm:ss', 'MST');
String Year = String.valueOf(dtMST.substring(0, 4));
String Month = String.valueOf(dtMST.substring(5, 7));
String Day = String.valueOf(dtMST.substring(8, 10));
String Hours = String.valueOf(dtMST.substring(11, 13));
String Mins = String.valueOf(dtMST.substring(14, 16));
String Secs = String.valueOf(dtMST.substring(17, 19));
system.debug('dtGMT'+dtGMT);
//Datetime format
system.debug('dtPST'+dtMST);
string stringDateFormat = Year + '-' + Month
+ '-' + Day+'T'+Hours + ':'
+Mins + ':' + Secs+'Z';
system.debug('stringDateFormat'+stringDateFormat);
//Query
query = ' select First_Name__c,Last_Name__c,State__c, '+
' Zip_Postal_Code__c,Comments__c ' +
' from Member_List_Stage__c '+
' where createdDate > '+stringDateFormat+'' ;
What conversion errors did you get?
Also, can you post the code that you were having problems with?
Datetime comparison is done using the following
YYYY-MM-DDTHH:MM:SS.000Z
So if the date time is 8:20:13 March-8-2010 then it should be
2010-03-08T20:20:13.00Z
(Note:- 00Z is for timezone and I believe that the default is GMT, you need to confirm that).
So following is the right query
SELECT Id, FirstName, LastName, createddate FROM Contact where createddate > 2010-01-07T06:56:39.00Z
If m getting you right, u want to change GMT time to current server time...
if this is the problem, solution is something like this:-
here im converting current datatime which in GMT format to user server format,
DateTime thour = System.now();
string mydtstring = thour.format('yyyy-MM-dd hh:mm:ss');
also you can get whatever format you want...
If I use dtGMT directly in the query , dtGMT value will be null in the quey. so, how to use datetime without converting.
global BatchMemberList (ID CampId, dateTime givenDateTime)
{
CampaignId=CampId;
dtGMT= givenDateTime;
}
global Database.Querylocator start(Database.BatchableContext BC)
{
//Query
query = ' select First_Name__c,Last_Name__c,State__c, '+
' Zip_Postal_Code__c,Comments__c ' +
' from Member_List_Stage__c '+
' where createdDate > dtGMT' ;
ok it means finaaly you want it in datetime format...this is d way to do it,
EX:
Datetime crtDatetime;
String crtDateStr = crtDatetime.format('yyyy-MM-dd HH:mm:ss');
Integer corpYear = Integer.valueOf(crtDateStr.substring(0, 4));
Integer corpMonth = Integer.valueOf(crtDateStr.substring(5, 7));
Integer corpDay = Integer.valueOf(crtDateStr.substring(8, 10));
Integer corpHours = Integer.valueOf(crtDateStr.substring(12, 13));
Integer corpMins = Integer.valueOf(crtDateStr.substring(14, 16));
Integer corpSecs = Integer.valueOf(crtDateStr.substring(17, 19));
DateTime corpBeginDate = DateTime.newInstance(corpYear, corpMonth, corpDay, corpHours, corpMins, corpSecs);
and now put corpBeginDate in ur query