Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have been trying to write a dynamic soql query by using soql-builder

http://richardvanhook.com/2010/04/11/soql-builder/

But, I am stuck while assigning date as criteria while constructing the query.

Here is the code.

Date endOfDate = Date.today();
Date startOfDate = Date.today().addMonths(-1);

Time startTime = Time.newInstance(0, 0, 0, 0);
Time endTime = Time.newInstance(23, 59, 59, 0);


DateTime startDate = DateTime.newInstance(startOfDate ,startTime);
System.debug('DEBUG: StartDate : '+startDate); // DEBUG: StartDate : 2014-03-07 05:00:00

DateTime endDate = DateTime.newInstance(endOfDate ,endTime);
System.debug('DEBUG: EndDate : '+endDate); //DEBUG: EndDate : 2014-04-09 03:59:59 

But, after query constructed by using soql-builder. Debug came like this :

(CreatedDate >= 2014-03-07T12:00:00Z AND CreatedDate <= 2014-04-07T11:59:59Z)

Actual:

startdate came 2014-03-07T12:00:00Z

endDate came 2014-04-07T11:59:59Z

Expected :

startdate 2014-03-07T00:00:00Z

endDate 2014-04-07T23:59:59Z

Anyone please give some light, why output is coming like this in soql-builder.

share|improve this question

1 Answer 1

up vote 6 down vote accepted

What's happening is that you're creating the times in your local time zone, but getting the string value in UTC. When I run your code, I get

StartDate : 2014-03-07 08:00:00
EndDate : 2014-04-08 06:59:59

because I'm on Pacific Time.

To get the results you're expecting, change DateTime.newInstance() to DateTime.newInstanceGmt(). This gives me the result

StartDate : 2014-03-07 00:00:00
EndDate : 2014-04-07 23:59:59
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.