You need to sign in to do that
Don't have an account?
Upton_X
Last Day of Month for DATETIME method
Hello - I have been able to find how to calculate the first day of the month with a DateTime method, however I am struggling with DateTime for last day of month and have not been able to find anything online.
I need datetime specifically because I can only query Case.CreatedDate with that datatype.
Line 8 Specifically
I need datetime specifically because I can only query Case.CreatedDate with that datatype.
Line 8 Specifically
trigger ScratchTrigger on Case (before insert) { //Write a trigger that will prevent each user from creating more than 99 cases a month //Create values for this month Date dayToday = Date.Today(); DateTime firstDayOfMonth = DateTime.newInstance(daytoday.toStartOfMonth().addMonths(-1), Time.newInstance(0,0,0,0)); DateTime lastDayOfMonth = DateTime.newInstance(dayToday.addMonths(1).toStartofMonth().addDays(-1)), Time.newInstance(0,0,0,0)); system.debug('Todays Date = '+dayToday+ ' firstDayOfMonth = '+firstDayOfMonth+ ' lastday of Month = '+lastDayOfMonth); //When a new case is crated, iterate through Trigger.New and add case to UserCreated Cases if(Trigger.isInsert){ List<Case> UserCreatedCases = [SELECT Id, CreatedById, CreatedDate FROM Case WHERE (CreatedDated >= :firstDayOfMonth) AND (CreatedDate <= :lastDayOfMonth)]; } }
By Date Method: To get the last day of the month: The output would be:
By DateTime Method: The output would be; you can modify this as per your requirements.
If you found this answer helpful mark this as best answers to help others.
Hi Malika-
Thanks for the suggestion. I am having a hard time with the origional value for dt taget which needs to be today() or Now() for date time.
Here is what I have that is producing an unexpcted outcome