12

wanted to get end date for Current Fiscal Quarter in apex any ideas, how to get this information.

20

Here is the simplest way I getting date I believe - We just need to query Period Object record and thats it. I think most of us are not aware of this Object in salesforce.com

Date closeDate = [Select EndDate From Period Where type = 'Quarter' and StartDate = THIS_FISCAL_QUARTER].EndDate;
  • 1
    Very elegant solution! – Doug B Nov 8 '12 at 11:56
4

Try the following, I just wrote it and tested for our fiscal year, worked ok...

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

Date fiscalYearStartDate;
Integer quarter;

if(system.today().month() >= FiscalYearStartMonth)
{
    fiscalYearStartDate = date.newinstance(system.today().year(), FiscalYearStartMonth, 1);
    quarter = ((system.today().month() - FiscalYearStartMonth) / 3) + 1;
}
else
{
    fiscalYearStartDate = date.newinstance(system.today().year() - 1, FiscalYearStartMonth, 1);
    quarter = ((12 + system.today().month() - FiscalYearStartMonth) / 3) + 1;
}    

Integer addMonths = quarter * 3;

Date lastDateOfThisQuarter = fiscalYearStartDate;

// this is the last date of the current quarter
lastDateOfThisQuarter = lastDateOfThisQuarter.addMonths(addMonths).addDays(-1);
2

//for standard calendar year

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);
system.debug(endOfQDate);
0

I use the below to determine the start month in a quarter.

Date d = Date.today(); // works for any date
Integer quarterStartMonth = (((Math.ceil((Decimal.valueOf(d.month()) / 12) * 4))*3) - 2).intValue();
Date quarterStart = Date.newInstance(d.year(), quarterStartMonth, 1);
  • You have not included any information outside of your code dump, and this solution will only work for standard fiscal quarters... – Adrian Larson Mar 22 '17 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.