alix-o
8/27/2013 - 1:06 AM

Programatically find fiscal year values in apex

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);