0

I need to find no of working days between CreatedDate of record and a new custom field of Date.I am using businesshours to find no of working days. the issue is CreatedDate will return DateTime object which having time also, where as the custom field has only Date. So when i find the no of working days between 5/17/2023, 3:24 AM and 5/18/2023 it returns 0, where as i am expecting to get 1. How to find the difference between dates only.

I have used date() on CreatedDate. it is working fine then, but i need to find any other solution is possible

1 Answer 1

0

I suspect you need to do the inverse and convert the custom date field to be a DateTime. Something like this where endDate is set to the value from the date field:

DateTime endDateTime = DateTime.newInstance(endDate, Time.newInstance(23, 59, 59, 999));

Then use the BusinessHours method and convert the result for Days, something like:

BusinessHours defaultBusinessHours = [SELECT Id FROM BusinessHours WHERE IsDefault = true];
Long milliseconds = BusinessHours.diff(defaultBusinessHours.Id, startDateTime, endDateTime);
Integer businessDays = (Integer)(milliseconds / (1000 * 60 * 60 * 24));

This should get a result of 1 for your example although I am unable to test it just now.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .