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
davidjbbdavidjbb 

SOQL

Hello Community,

 

From my system.debug I got the following: 

2012-08-17 17:16:00

I am trying to do a SOQL on DateTime i.e

 where (jbbfc2__Start_Date__c >= :getFromDate() and jbbfc2__finished_date__c <= :getToDate())

 

However, my SOQL isn't taking into consideration of the dates and I believe it's the dateTime from getFromDate() and getToDate()

 

                //Get From Date

                public datetime getFromDate(){                  

                        System.debug('FROM DATE'+ controllerObject.Start_Date__c);

                        return this.fromDate = controllerObject.Start_Date__c;

                        

                }

 

How do I convert the DateTime to the following, so the query will work?

 

  • YYYY-MM-DDThh:mm:ss+hh:mm
  • YYYY-MM-DDThh:mm:ss-hh:mm
  • YYYY-MM-DDThh:mm:ssZ
ShuchiMShuchiM

Did you try the datetime method: 'format' ?

Here are the details: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_datetime.htm

davidjbbdavidjbb

Don't think that's going to work because it'll try to bind a string return type to the start_date__c which is date time.

what's the solution to getting around that if i use 

.format('yyyy-MM-dd\'T\'hh:mm:ss\'Z\'')

 

 

ShuchiMShuchiM

How about something like this: 

String mydate = DateTime.now().format('yyyy-MM-dd\'T\'hh:mm:ss\'z\'');
System.debug('...' +Database.query('select Id from Account where createddate <' + mydate + ' limit 10'));

hisalesforcehisalesforce

You cannot compare datetime and date in SOQL.

So best way is to convert datetime to date and do your comparing.

 

Datetime dt=System.now();

 

Date test=Date.newinstance(Dt.year(),DT.month(),DT.Day());

 

Hope this will  help to solve your issue.

davidjbbdavidjbb

Finished_Date__c is a datetime field. I'm not trying to compare a datetime to a date.