+ Start a Discussion
Goodluck123Goodluck123 

Passing datetime to query in apex class dynamically.

Select Id from objectname__c where source__c = SF and lastmodifieddate <  2010-02-03T00:15:00.000Z

 

This query is working fine in eclipse, but I want the same query passing datetime to query in apex class dynamically.

 

 

Please let me know how to frame my query in apex class.

 

Thanks in Advance..

 

PK

 

magdielhfmagdielhf
I've been looking for some functions that works for SOQL regarding Dates, but i couldn't find anything, so 

I had to work with Dates from Apex and pass it to my SOQL
 
public List<Account> getAccountsByDate(Date dateFrom, Date dateTo)
{
     return [Select Id, Name
                From Account
                Where CreatedDate >= :dateFrom
                          And CreatedDate <= :dateTo];
Message Edited by magdielhf on 02-08-2010 12:13 PM
Goodluck123Goodluck123

Thanks for your reply,

 

What is the format for dateFrom and dateTo in the  parameters you mentioned.can you please explain me how you are doing.

magdielhfmagdielhf

Hi,

 

I think that depends on your needs and use case, if you need a DateTime or a Date only, the requirements for those dates ranges, so far I'm using these methods and formats for conversion using Apex, 

 

 

DateTime 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_datetime.htm

 

Date 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_date.htm

 

Here you might find some suitable information of how to parse Dates,

 

Regards. 

Goodluck123Goodluck123

Thanks,

 

Let me research on this, will update you after fixing my issue.

 

TerryLuschenTerryLuschen

I found the answer on this web site: Link to Answer

 

Code can look like this...

datetime dtStart;
datetime dtEnd;

 

dtStart = dateTime.now().addDays(-1);
dtEnd = dateTime.now()
extraWhereForQuery = ' AND CreatedDate >= ' + dtStart.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'') +
             ' AND CreatedDate <= ' + dtEnd.format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');