Skip to content

Instantly share code, notes, and snippets.

@mxalix258
Created August 27, 2013 01:06
Show Gist options
  • Save mxalix258/6348569 to your computer and use it in GitHub Desktop.
Save mxalix258/6348569 to your computer and use it in GitHub Desktop.
Programatically find fiscal year values in apex
//for loop to find Opportunity Stages that are Closed and Won
for(OpportunityStage oppstage : oppstages){
if(oppstage.IsClosed == True && oppstage.IsWon == True){
wonstages.add(oppstage.MasterLabel);
}
}
Integer currentFY;
Integer currentFYMonth;
Integer CurrentFYDays;
Date today;
Date FYStartDate;
Date FYEndDate;
Date LastFYStartDate;
Date LastFYEndDate;
//Selects organization info from organization that matches the current user
Organization orgInfo = [SELECT FiscalYearStartMonth, UsesStartDateAsFiscalYearName
FROM Organization
WHERE id=:Userinfo.getOrganizationId()];
today = system.today();
currentFYMonth = orgInfo.FiscalYearStartMonth;
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();
}
}
CurrentFYDays = date.daysInMonth(currentFY, currentFYMonth);
FYStartDate = date.parse(CurrentFYMonth + '/' + '01' + '/' + currentFY);
FYStartDate = FYStartDate.addYears(-1);
System.Debug('The current FY start date is ' + FYStartDate);
LastFYStartDate = FYStartDate.addYears(-1);
System.Debug('The last FY start date is ' + LastFYStartDate);
FYEndDate = FYStartDate.addYears(1).addDays(-1);
System.Debug('The current FY end date is ' + FYEndDate);
LastFYEndDate = FYEndDate.addYears(-1);
System.Debug('The last FY end date is ' + LastFYEndDate);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment