You need to sign in to do that
Don't have an account?
Need help writing a test class for an invocable method
Hi everyone,
With a lot of help from this forum, I got my first invocable method to work but now I am stuck on writing the test class for it.
Here's my invocable method and the attempted test class:
global class CreatedDateWithinBusinessHours {
@InvocableMethod(label = 'Created Date is within BH' description = 'Check if flow record Created Date is within business hours')
public static List<Results> execute(List<Requests> requestList) {
// Get the default business hours
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault = true];
// Pass the Created Date Time of the flow Record
Datetime targetTime = requestList[0].inputCreatedDateTime;
// Boolean that returns true if date time is within Business hours
Boolean isWithin = BusinessHours.isWithin(bh.id, targetTime);
// Results object that holds the return values
Results response = new Results();
//Add the return values to the Results object
response.outputMember = isWithin;
//Wrap the Results object in a List container
List<Results> responseWrapper = new List<Results> ();
responseWrapper.add(response);
return responseWrapper;
}
global class Requests {
@InvocableVariable(label = 'Created Date Time' description = 'Created Date of the Flow record' required = false)
global Datetime inputCreatedDateTime;
}
global class Results {
@InvocableVariable(label = 'Within Business Hours' description = 'Boolean that returns true if the Created Date is within Business Hours' required = true)
public Boolean outputMember;
}
}
@isTest
private class CDwithinBusinessHoursTest {
static testMethod void validateCDBusinessHours() {
Case c = new Case ();
Datetime recordCD = c.CreatedDate;
insert c;
List<Datetime> requestList = new List<Datetime>();
requestList.add(recordCD);
Test.startTest();
//Getting this error here: Method does not exist or incorrect signature: void execute(List<Datetime>) from the type CreatedDateWithinBusinessHours
CreatedDateWithinBusinessHours.execute(requestList);
Test.stopTest();
//Variable does not exist: outputMember
System.assert(outputMember);
}
}
Please try the below test class:
Thanks,
Maharajan.C
All Answers
Please try the below test class:
Thanks,
Maharajan.C