Adminstrator · Apex · Salesforce

How to use DateTime in Dynamic SOQL Query Where Condition?

There are many place we use DateTime in Where condition. There are many people having trouble to use developer console or dynamic SOQL query to pull data based on DateTime Field.

Salesforce always store date/datetime field in to GMT format. GMT Format is “yyyy-MM-ddThh:mm:ssZ”.

User Case: you may want to check how many account create past 5 days. you don’t have any Date Literals to accomplish above use case. You need to build your custom logic like below

below query for past few days :

SELECT Id FROM Account WHERE  CreatedDate > 2016-09-10T00:00:00Z

below query for particular day:

SELECT Id FROM Account WHERE  CreatedDate >= 2016-09-10T00:00:00Z and CreatedDate <= 2016-09-10T23:59:59Z

 

 

One thought on “How to use DateTime in Dynamic SOQL Query Where Condition?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s