Skip to content

Instantly share code, notes, and snippets.

Last active August 24, 2023 14:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cchrisv/826f4c1c0909322132d7e3c1e66301ed to your computer and use it in GitHub Desktop.
Save cchrisv/826f4c1c0909322132d7e3c1e66301ed to your computer and use it in GitHub Desktop.
global class CalculateBusinessHoursDiff {
global class request
@InvocableVariable(label='Business Hours Id' required = true)
global String businessHoursId;
@InvocableVariable(label='Start Date' required = true)
global DateTime startDate;
@InvocableVariable(label='End Date' required = true)
global DateTime endDate;
global class response
@InvocableVariable (label='Result (in millisecond)')
global long diffResult;
@InvocableVariable (label='Result (in second)')
global long diffResultSec;
@InvocableVariable (label='Result (in minute)')
global long diffResultMin;
@InvocableMethod(label='Professor Flow | Calculate Business Hours Difference' description='Returns the difference in milliseconds,second or minute between a start and end Datetime based on a specific set of business hours.')
global static List<response> getMilliSecondDiff(List<request> inputParams)
List<response> finalList = new List<response>();
long diffinms = 0;
//use utility method in businesshours
if(inputParams != null && inputParams.size() > 0)
for(request pf: inputParams)
diffinms = BusinessHours.diff(pf.businessHoursId,pf.startDate,pf.endDate);
response resp = new response();
resp.diffResult = diffinms;
resp.diffResultSec = diffinms / 1000 ;
resp.diffResultMin = diffinms / 60000 ;
return finalList;
global class CalculateBusinessHoursDiffTest
@isTest static void check_diffInMilliSeconds()
Profile sysadminprofile = [select Id, name from profile where Name='System administrator' limit 1];
User adminUser = [select Id,Name from user where profileId= :sysadminprofile.Id and isactive= True limit 1];
BusinessHours bhoursId = [select Id from BusinessHours where Name='Default' limit 1];
List<CalculateBusinessHoursDiff.request> inputparams = new List<CalculateBusinessHoursDiff.request>();
CalculateBusinessHoursDiff.request pf = new CalculateBusinessHoursDiff.request();
pf.startDate =;
pf.endDate =;
pf.businessHoursId = bhoursId.Id;
List<CalculateBusinessHoursDiff.response> response = CalculateBusinessHoursDiff.getMilliSecondDiff(inputparams);
Integer total_duration_in_seconds = (response[0].diffResult/1000).intValue();
System.assertEquals(86400,total_duration_in_seconds );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment