8

I need to check if close date of opportunity is equal to current fiscal year in apex.Can someone explain me how to use get current fiscal year.FYI my Org doesn't have custom Fiscal year setup.(Its Standard Fiscal year).

2 Answers 2

7

You can get the month of your organisation fiscal year with this query :

Integer orgFiscalMonth = [SELECT FiscalYearStartMonth FROM Organization].FiscalYearStartMonth;

And create a Date variable using this :

Date orgFiscalYear = Date.newinstance(system.today().year(), orgFiscalMonth, 1);
14

I like this approach better, because it will work in whichever fiscal year setup you're in. It makes use of an SFDC table that is not very well known:

String currentFiscalYear = [SELECT FiscalYearSettings.Name FROM Period WHERE Type = 'Year' AND StartDate <= TODAY AND EndDate >= TODAY].FiscalYearSettings.Name;
4
  • The cool thing about this query is you can easily determine which fiscal period you're in as well, and the year of the fiscal period: Select Type, StartDate, EndDate, Number, FiscalYearSettings.PeriodId, From Period Where Type='Quarter' and startDate <= TODAY and endDate >= TODAY
    – DaveS
    Commented Oct 29, 2015 at 8:49
  • 1
    I also prefer this method for the OP's use case, but it's risky for determining fiscal quarters/years of a generic date. The Period object is populated automagically by SF via what seems to be an internal, system-wide trigger on all objects. Anytime a record is created (excluding those made in unit tests) containing a date field with a value set to a year not represented by any Period records then new, corresponding Period records are generated. Thus, if the filter in the SOQL query above were tied to a generic date with a year not represented by any Period records you could have problems. Commented Jul 29, 2016 at 20:36
  • How do you substitute Today with a date if one is looking for future Fiscal year, unless one formats the date [YYYY,MM,DD]. Any ideas?
    – mubs
    Commented Sep 9, 2019 at 23:11
  • Hi Mubeen. Probably better chance of success if you ask a new question on salesforce stackexchange for this. That way we can get the question clear and have more people look at it. Commented Sep 10, 2019 at 9:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .