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 get current fiscal year start date and end date using apex

I have requirement to fetch records for current fiscal year through soql in apex.
        currentFiscalYear = [SELECT FiscalYearSettings.Name,StartDate,EndDate FROM Period WHERE Type = 'Year' AND StartDate <= TODAY AND EndDate >= TODAY].FiscalYearSettings.Name;

I am using soql select id,name from Service where CreatedDate >= currentFiscalYear.StartDate and CreatedDate <= currentFiscalYear.EndDate which is giving error System.QueryException: Variable does not exist: currentFiscalYear.StartDate
 
Suraj Tripathi 47Suraj Tripathi 47
Hi Sandy,
You're getting errors because you didn't define the variable currentFiscalYear above, you have directly used it.

This should be like this -

String currentFiscalYear = [SELECT FiscalYearSettings.Name FROM Period WHERE Type = 'Year' AND StartDate <= TODAY AND EndDate >= TODAY].FiscalYearSettings.Name;

Please mark it as the best answer, if it helps in any way.

Thanks.