Skip to content

Instantly share code, notes, and snippets.

@cchrisv
Last active April 24, 2024 14:08
Show Gist options
  • Star 6 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 ;
finalList.add(resp);
}
}
return finalList;
}
}
@isTest
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];
System.runAs(adminUser)
{
List<CalculateBusinessHoursDiff.request> inputparams = new List<CalculateBusinessHoursDiff.request>();
CalculateBusinessHoursDiff.request pf = new CalculateBusinessHoursDiff.request();
pf.startDate = Datetime.now();
pf.endDate = Datetime.now().addDays(1);
pf.businessHoursId = bhoursId.Id;
inputparams.add(pf);
Test.startTest();
List<CalculateBusinessHoursDiff.response> response = CalculateBusinessHoursDiff.getMilliSecondDiff(inputparams);
Integer total_duration_in_seconds = (response[0].diffResult/1000).intValue();
Test.stopTest();
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