ShowAll Questionssorted byDate Posted
Ben Merton 15

# Quite complex date/time and business hours question

Supposing I have a Work Order with Completion_Date_Time__c and I am trying to work out the Date_Time of material required based both on a Delivery_Time__c AND the Business Hours / Holidays.

So working backwards it would be just Completion_Date_Time__c MINUS the Delivery_Time__c MINUS the NON-Business Hours/Holidays.  But what if that calculation AGAIN lands on a holiday / crosses a holiday.  You would need to parse this out?  Or am I completely missing something?

Are there any methods that can perform this calculation?

pcon
Assuming you have all of your business hours and holidays in the system correctly (and are not assuming that salesofrce will know about the holidays), you can use the BusinessHours class [1] to do the math for you.  So with your fields above if Deliver_Time__c is in days you'll need to covert days to milliseconds and then subtract that to your Completion Date Time field.

```BusinessHours bh = [
select Id
where Name = '24x7' // Or whatever criteria you are using
];

Long daysInMilli = Delivery_Time__c *
24 * // Hours per day
60 * // Minutes per hour
60 * // Seconds per minute
1000; // Milliseconds per second