Skip to content

Instantly share code, notes, and snippets.

@cchrisv
Last active March 20, 2020 02:56
Show Gist options
  • Save cchrisv/9fd2397961739551349f417640755957 to your computer and use it in GitHub Desktop.
Save cchrisv/9fd2397961739551349f417640755957 to your computer and use it in GitHub Desktop.
global class calculateIsInsideBusinessHours {
global class request
{
@InvocableVariable (label='Date' required = true)
global DateTime givenDateTime;
@InvocableVariable (label='Business Hours Id' required = true)
global String businessHoursId;
}
global class response
{
@InvocableVariable (label='Is within Business Hours?')
global Boolean dateInsideBusinessHours;
}
@InvocableMethod(label='Professor Flow | Calculate if date inside Business Hours' description='Returns true if the specified target date occurs within business hours. Holidays are included in the calculation.')
global static List<response> DateInBusHours (List<request> inputParams)
{
//initialize response
List<response> responseList = new List<response>();
Boolean checkResult;
if(inputParams != null && inputParams.size()> 0)
{
for(request pf : inputParams)
{
checkResult = BusinessHours.isWithin(pf.businessHoursId, pf.givenDateTime);
response rs = new response();
rs.dateInsideBusinessHours = checkResult;
responseList.add(rs);
}
}
return responseList;
}
}
@isTest
global class calculateIsInsideBusinessHoursTest
{
@isTest static void CheckDateInsideBusinessHours()
{
// Implement test code
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<calculateIsInsideBusinessHours.request> inputparams = new List<calculateIsInsideBusinessHours.request>();
calculateIsInsideBusinessHours.request pf = new calculateIsInsideBusinessHours.request();
pf.givendateTime = Datetime.now();
pf.businessHoursId = bhoursId.Id;
inputparams.add(pf);
Test.startTest();
List<calculateIsInsideBusinessHours.response> response = calculateIsInsideBusinessHours.DateInBusHours(inputParams);
Test.stopTest();
system.assertEquals(true, response[0].dateInsideBusinessHours);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment