[SalesForce] String / Date comparisons Custom Fiscal Year

I have custom fiscal year starting May 1st to April 30th.
I want to find a clever way to select a picklist value based on opportunity closed date.

Picklist values 2010 through 2020

Here is the scenario, If the close date on or after May 1 2014, select 2015 from the picklist if it is March 2014, select 2014 from picklist.. etc. any help greatly appreciated

PS, I am trying to do this in a custom controller.

Best Answer

You didn't answer my comment about "is it real custom fiscal year (does the Period table have any entries) or is it standard Gregorian with shifted month start".

Either way I would discourage you from hardcoding the "May 1st" boundary date. Here's how to get the reference data if it's a real custom fiscal year (my company uses 5-4-4 calendar)

SELECT FiscalYearSettings.Name, Type, StartDate, EndDate 
FROM Period
WHERE Type = 'Year'

enter image description here

As you can see the dates jump like crazy - no way I'm hardcoding them anywhere, I need to fetch them from this reference table (bonus points for the fact that you can fetch "FY2015" straight from the table if that's how they called the year label).

And if it's a standard Gregorian fiscal year but shifted - you should query the start month from Org settings:

SELECT FiscalYearStartMonth FROM Organization

Anyway.

For the first case I'd probably fetch them all once and loop through them until I find a record where my date falls between the start & end date. Simple, boring, generic solution - sometimes these are best.

For the second case I'd reuse the suggestion I've given to sfdc_ninja:

Integer startMonth = 5; // don't hardcode it

/*  trigger: before insert, before update
    for(Opportunity o : trigger.new)
    etc etc
*/

Date d = System.today(); // o.CloseDate
Integer year = d.year() + (d.month() >= startMonth ? 1 : 0);

String yearAsString = 'FY' + String.valueOf(year);
System.debug(yearAsString); // outputs "FY2015"
Related Topic