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
sandy hellosandy hello 

How to pass current fiscal year or current year dynamically in SOQL query to fetch records created in the current year

I had a requirement to run batch job on records created current fiscal year/current year and the job is executed twice in a year. How to pass curent fiscal year/current year dynamically in soql to fetch records created in current fiscal year.
KdKomalKdKomal
Hi Sandy,

In you batch class's start method you can get the start date and the end date of the current fiscal year using the below query - 
Period currentFiscalYear = [SELECT StartDate, EndDate FROM Period WHERE Type = 'Year' AND StartDate <= TODAY AND EndDate >= TODAY LIMIT 1];

Once you have the currentFiscalYear, all you need to do is in your start method is 
Database.getQueryLocator([Select <FIELDS> FROM <ObjectName> Where CreatedDate >=: currentFiscalYear.StartDate and CreatedDate <=: currentFiscalDate.EndDate]);

I hope this helps.