+ Start a Discussion


Hi people!


i need to help someone in buliding my SOQL query


i have to fetch records from an object for next three months i can explain the scenerio below:


Consider the current month is July i have to fetch the records for July
after that i have to fetch records for August and at the end i have to fetch the records for September


and EMail these list to users each month


i am very much confused about soql queries like how can i get record for each month and then next month and after next month seperately PLEASE HELP.




Hello Ahmed,


Have these records been created in the month of July or edited? Accordingly you can filter the records in your query on the basis of created_date or last_modified_date like:


Select a.CreatedDate, a.Id, a.LastModifiedDate 
from Account a 
where   a.CreatedDate > 2013-07-31T00:00:00Z and a.CreatedDate < 2013-01-01T00:00:00Z 



I would suggest you one more solution for this requirement, as you need to email the records monthly. If possible create a report to fetch these records and schedule this report to run monthly and while scheduling you can provide the email ids.




hey thanks bro i will check if it helps


Do like this


In your case

For July - SELECT Id FROM YourObject WHERE CreatedDate = THIS_MONTH

For August - SELECT Id FROM YourObject WHERE CreatedDate = NEXT_MONTH

For September - Do it like this


datetime myDate = System.Today();
Integer mon = myDate.addmonths(2).month();
[SELECT id FROM YourObject WHERE CALENDAR_MONTH(CreatedDate) = :mon];


You can change the date field which you need as your liking.


If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.