wanted to get end date for Current Fiscal Quarter in apex any ideas, how to get this information.
4 Answers
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
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);
//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);
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 ♦Commented Mar 22, 2017 at 20:20