Jul 30, 2012

Is There Better Way To Get Last Date Of Fiscal Quarter?

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

//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.


  1. Recently had to do something similar. (Word of warning: to query the organization, the user will need to have the view all data permission. We used a 'without sharing' class to query the org.)

    // get fiscal year start month
    Integer FiscalYearStartMonthIndex = [select FiscalYearStartMonth
    from Organization
    where id=:Userinfo.getOrganizationId()].FiscalYearStartMonth;

    // find end of first quarter of current year
    Date quarterEnd = Date.newInstance(system.today().year(), FiscalYearStartMonthIndex, 1).addMonths(3).addDays(-1);

    // if we are past the current quarter, move to the next one.
    while (system.today() >= quarterEnd) {
    quarterEnd = quarterEnd.addMonths(3);

    return quarterEnd;

  2. This comment has been removed by the author.

  3. I think you can also use the Math.mod to do the required transformation:

    public class FinancialQuarterTool {

    public static void example(){

    Integer fiscalYearStartMonth = [select FiscalYearStartMonth from Organization where id=:Userinfo.getOrganizationId()].FiscalYearStartMonth;


    public static Date firstDateOfFq(Date currentDate, Integer fiscalYearStartMonth ){

    Integer offset = currentDate.month() + 12 - fiscalYearStartMonth;
    offset = Math.mod(offset, 3);
    return currentDate.addMonths(-offset).toStartOfMonth();


    public static Date lastDateOfFq(Date currentDate, Integer fiscalYearStartMonth ){

    Date d = firstDateOfFq(currentDate, fiscalYearStartMonth );
    return d.addMonths(3).addDays(-1);



  4. Did you try with the Period object?

  5. Date dt = date.newInstance(1014,12, 17);
    Integer currentMnt =dt.month();
    Integer currentQ =((currentMnt-1)/3) + 1;
    Date endOfQDate = date.newInstance(dt.year(),currentMnt + (4 - (currentMnt - ((currentQ -1)*3))) , 1).addDays(-1);

  6. Thank you for posting such topics. Very good, I think I found the knowledge I needed

  7. I am always eager to catch hold of the new posts being published on your website, because of this i use to updated, thanks for sharing this wonderful article.
    Digital Marketing Company in Delhi
    SEO Company in Delhi
    Mobile App Development Company in Lucknow

  8. Thanks for providing such a great Information, you can see, we also provide
    Peptide Synthesis

  9. How to get first date by passing w.r.t any date in method

  10. Everything is possible. We can install last date function in website and software from your service. Listen, here is also opportunity to get Lock repair in Jacksonville service and resolve the issues.