Recently it was requested to get the "Last Date" of present Fiscal Quarter. For e.g. if "Fiscal Year Start Month" is set to January (Setup > Administration Setup > Company Profile > Fiscal Year) and present month is Feb then date returned should be "31 March 2012"
SOQL is not the option as in the end I need to get the Date so after lot of brain storming end up with this solution, if this is not the best solution then other solutions suggestions are most welcomed.
So here is my apex method which returns me the last date of present fiscal quarter
Code is self explanatory, still if anything is not clear do reach me out.
SOQL is not the option as in the end I need to get the Date so after lot of brain storming end up with this solution, if this is not the best solution then other solutions suggestions are most welcomed.
So here is my apex method which returns me the last date of present fiscal quarter
//Returns last date of present fiscal quarter
public Date LastDateofCurrentFQ()
{
// Info : There are 4 Fiscal Quarter FQ( each of 3 months) in fiscal year
Organization org = new Organization() ;
org = [select FiscalYearStartMonth from Organization where id=:Userinfo.getOrganizationId()];
Integer FiscalYearStartMonthIndex = org.FiscalYearStartMonth ;
// Month Index like january : 1 , february: 2 and so on
Integer CurrentMonthIndex = system.today().month();
if(CurrentMonthIndex < FiscalYearStartMonthIndex )
CurrentMonthIndex = 12+ CurrentMonthIndex ;
Integer DifferenceofBothMonths = CurrentMonthIndex - FiscalYearStartMonthIndex ;
Integer CurrentFiscalQuarterLastMonth = 0;
if(0 < = DifferenceofBothMonths && DifferenceofBothMonths <= 2 ) //For first quarter
CurrentFiscalQuarterLastMonth = FiscalYearStartMonthIndex +2 ;
else if( 3 <= DifferenceofBothMonths && DifferenceofBothMonths <= 5) //For second quarter
CurrentFiscalQuarterLastMonth = FiscalYearStartMonthIndex +5 ;
else if( 6 <= DifferenceofBothMonths && DifferenceofBothMonths <= 8) // For third quarter
CurrentFiscalQuarterLastMonth = FiscalYearStartMonthIndex +8 ;
else if( 9 <= DifferenceofBothMonths && DifferenceofBothMonths <= 11) // For fourth quarter
CurrentFiscalQuarterLastMonth = FiscalYearStartMonthIndex +11 ;
if(CurrentFiscalQuarterLastMonth > 12)
CurrentFiscalQuarterLastMonth = CurrentFiscalQuarterLastMonth - 12 ;
Date firstDateOFyear = date.newInstance(system.today().year(), 1, 1);
Date LastDateofCurrentFQ = date.newInstance(system.today().year(), 1, 1);
LastDateofCurrentFQ = firstDateOFyear.addMonths(CurrentFiscalQuarterLastMonth).addDays(-1) ;
return LastDateofCurrentFQ ;
}
Code is self explanatory, still if anything is not clear do reach me out.
