ProfessorFlow
ActionActive

Calculate Business Hours Difference

Calculate the difference between two DateTimes counting only business hours. Returns results in milliseconds, seconds, and minutes.

v1.0.0Updated yesterday

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

  1. Deploy the CalculateBusinessHoursDiff Apex class to your Salesforce org using SFDX
  2. In Flow Builder, add the "Calculate Business Hours Difference" action element
  3. Pass the Business Hours record ID, start DateTime, and end DateTime
  4. Read the output in your preferred unit — milliseconds, seconds, or minutes
  5. Use the result in assignments, decisions, or downstream actions

Inputs & Outputs

Input3 attributes
businessHoursIdRequired
Text

The record ID of the Business Hours to evaluate against (from Setup > Business Hours)

startDateRequired
DateTime

The beginning of the time range to measure

endDateRequired
DateTime

The end of the time range to measure

Output3 attributes
diffResult
Number

Difference between start and end dates in milliseconds, counting only business hours

diffResultSec
Number

Difference between start and end dates in seconds, counting only business hours

diffResultMin
Number

Difference 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 classes
  • CalculateBusinessHoursDiffTest.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.

Search

Search components, articles, and tags