when i write a trigger I have facing a issue related to quarter. In apex code i want to check particular given date is Which Quarter.please anyone suggests...
-
check this out.. salesforce.stackexchange.com/questions/4166/…– Ratan PaulCommented Feb 5, 2016 at 5:11
-
1the answer depends on whether you are using calendar year fiscal quarters or some the quarters are not based on calendar year (e.g. Q1 starts on Feb 1). You can also use formula fields for this– cropredyCommented Feb 8, 2016 at 6:52
Add a comment
|
2 Answers
You can use Date.Month()
for the the month value, here you will get integer value like 1 for Jan...12 for Dec then from this value you can easily get its quarter.
Here is example for you:
Integer quarter = date.today().month()/3;
if(quarter == 0)
system.debug('==FirstQuarter==';
else if(quarter == 1)
system.debug('==SecondQuarter==');
else if (quarter == 2)
system.debug('==ThirdQuarter==');
else
system.debug('==fourthQuarter==');
Hope this helps!!!
Regards, Krishna
-
I guess that when you say
month == 0
, you meani==0
? That's why the variable should be named 'quarter' instead :)– mkormanCommented Feb 5, 2016 at 10:33 -
-
1Thats incorrect, For June , it is 6. How 6/3 = 2 is third quarter? Commented Sep 17, 2017 at 5:25
Use the Apex code below.
for (integer month = 1; month <= 12; month++) {
system.debug(month + ' = ' + math.round((month+1)/3.0));
}
-
3
-
There is no magic number here. 3.0 is number of months in a quarter. When you add 1 to the month number and divide it by 3.0 (to make the expression a double data type) and round it. You get the quarter number. Commented Oct 17, 2017 at 16:26