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
Rakul SrivastavRakul Srivastav 

need the query to fetch records between a fixed time-8 p.m PST.

How can i get records for a query where i need to get the records which are created between 8p.m PST the previous day to 8p.m PST today. Please help me with the query in Apex.

I have a date field as '2021-05-17T22:00:59.000+0000', now i need to check if that field falls in between Yesterday 8p.m PST to today's 8p.m PST. and yesterday and today is not a fixed date, i need to use it in query. 

I could have used "SELECT Id,CreatedDate FROM Account WHERE CreatedDate >= 2021-06-21T20:00:00Z AND CreatedDate <= 2021-06-22T20:00:00Z" but it is for a fixed date. i want it to be dynamic.

 i want here yesterday and today which is dynamic and time in 8p.m PST. daily emails to be sent for the past 1 day between 8pm PST the previous day to 8p.m today
I'm new to Salesforce, please guide me.

Best Answer chosen by Rakul Srivastav
ShivankurShivankur (Salesforce Developers) 
Hi Rakul,

You can use a logic like below to get the accounts created within last 24 hours :
DateTime currTime = System.now();
DateTime 24hrBefore = currTime.addHours(-24);

List<Account> acc = [SELECT Id,CreatedDate FROM Account where CreatedDate < :currTime and CreatedDate> 24hrBefore];
For this to run at 8 p.m. PST, you might need to run this logic or apex in scheduled manner.

Check out the example given over below helper documentation to understand how you can fit it with your requirement and learn more about it :
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
https://trailhead.salesforce.com/content/learn/modules/asynchronous_apex/async_apex_scheduled

Hope above information helps. Please mark as Best Answer so that it can help others in future.

Thanks.