Calculate Business Hours Difference
Calculate the difference between two DateTimes counting only business hours. Returns results in milliseconds, seconds, and minutes.
Overview
Calculate Business Hours Difference is an Apex invocable action that measures the elapsed time between two DateTimes while only counting time that falls within a configured set of Business Hours. It wraps Salesforce's native BusinessHours.diff() method and returns the result in three units — milliseconds, seconds, and minutes — so your Flow can use whichever is most convenient.
This is the complement to the Add Business Hours action. Where Add Business Hours projects a future date by adding time, this action measures how much working time actually passed between two known points.
When to Use This
- Measure actual SLA elapsed time between case creation and resolution
- Calculate working-hours duration for time-tracking or billing
- Determine how long a record sat in a particular status during business hours
- Compare actual response times against SLA targets
When NOT to Use This
- For simple elapsed-time math that does not need to exclude nights and weekends, use a Flow formula with date subtraction
- If you need the difference across multiple different Business Hours records in a single transaction, you will need a custom collection-based approach
- For projecting a future DateTime by adding an interval, use the Add Business Hours action instead
User Guide
Getting Started
- Deploy the
CalculateBusinessHoursDiffApex class to your Salesforce org using SFDX - In Flow Builder, add the "Calculate Business Hours Difference" action element
- Pass the Business Hours record ID, start DateTime, and end DateTime
- Read the output in your preferred unit — milliseconds, seconds, or minutes
- Use the result in assignments, decisions, or downstream actions
Inputs & Outputs
TextThe record ID of the Business Hours to evaluate against (from Setup > Business Hours)
DateTimeThe beginning of the time range to measure
DateTimeThe end of the time range to measure
NumberDifference between start and end dates in milliseconds, counting only business hours
NumberDifference between start and end dates in seconds, counting only business hours
NumberDifference between start and end dates in minutes, counting only business hours
Troubleshooting
Result is zero or unexpectedly small: Verify that both the start and end DateTimes fall within periods covered by the selected Business Hours record. If both times are outside business hours (e.g., both on a weekend), the difference will be zero.
Negative result: The action calculates end - start. If your end DateTime is before your start DateTime, the result will be negative. Ensure you pass the earlier date as startDate.
Business Hours ID not found: Make sure you are passing a valid 18-character record ID for the BusinessHours object. Query it with a Get Records element or use a constant if your org has a single set of business hours.
Code
Source code is available on GitHub. The component consists of two Apex classes in a standard SFDX project structure:
CalculateBusinessHoursDiff.cls— The invocable action with Request/Response inner classesCalculateBusinessHoursDiffTest.cls— Test class validating the millisecond-to-second conversion against default business hours- Metadata XML files configured for API version 66.0
Deploy to your org using SFDX:
sf project deploy start --source-dir force-app
Release History
See the release timeline above for version history.