Skip to main content
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.
1 respuesta
  1. 27 sept 2021, 19:34
    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.