Skip to content

Instantly share code, notes, and snippets.

@boxfoot
Created April 6, 2012 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save boxfoot/2322443 to your computer and use it in GitHub Desktop.
Save boxfoot/2322443 to your computer and use it in GitHub Desktop.
Determine current fiscal year in Apex
/* (c) Benj Kamm 2012
* Determine current fiscal year in Apex -- add this method to a class that needs it.
*
* FiscalYearStartMonth - Integer 1-12 corresponds to starting month of FY
* UsesStartDateAsFiscalYearName - If false, FY Name is year of ending month; if true, name from beginning month.
*/
private integer getCurrentFY() {
Organization orgInfo = [SELECT FiscalYearStartMonth, UsesStartDateAsFiscalYearName
FROM Organization
WHERE id=:Userinfo.getOrganizationId()];
Date today = system.today();
Integer currentFY;
if (today.month() >= orgInfo.FiscalYearStartMonth) {
if (orgInfo.UsesStartDateAsFiscalYearName) {
currentFY = today.year();
} else {
currentFY = today.year() + 1;
}
} else {
if (orgInfo.UsesStartDateAsFiscalYearName) {
currentFY = today.year() - 1;
} else {
currentFY = today.year();
}
}
return currentFY;
}
@capeterson
Copy link

Thanks for this! Amazingly helpful.

@guyclairbois
Copy link

Here's a simpler and shorter approach:
String currentFiscalYear = [SELECT FiscalYearSettings.Name FROM Period WHERE Type = 'Year' AND StartDate <= TODAY AND EndDate >= TODAY].FiscalYearSettings.Name;

Copy link

ghost commented Sep 27, 2018

Here's a simpler and shorter approach:
String currentFiscalYear = [SELECT FiscalYearSettings.Name FROM Period WHERE Type = 'Year' AND StartDate <= TODAY AND EndDate >= TODAY].FiscalYearSettings.Name;

Amazing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment