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'
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"
Best Answer
You can get the month of your organisation fiscal year with this query :
And create a Date variable using this :