You need to sign in to do that
Don't have an account?
Pass Record Created Date from Flow to Apex and return boolean output variable
Hi everyone,
I'm completely new to Apex and invocable methods so any help would be greatly appreciated.
I'm trying to create an invocable method that uses the Business Hours class. I'd like to pass the Created Date of a record in a record-triggered flow into Apex and check if the Created Date is within Business Hours. I would then like to return a boolean as the output variable that I can then use in a decision element to trigger a record creation.
Here's what I have pasted together from what I could find via help articles and a previous post:
global class CreatedDateWithinBusinessHours {
@InvocableMethod(label = 'Check if within BH' description = 'Check if record Created Date is within business hours')
public static List<Results> execute(List<Requests> requestList) {
List<SObject> inputCollection = requestList[0].inputCollection;
// Get the default business hours
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault = true];
// Is this where I would use my Created Date input variable I'm passing from the flow?
Datetime targetTime = Datetime();
// Find whether the time is within the default business hours. Is this the boolean I would use as the output variable to use in my flow?
Boolean isWithin = BusinessHours.isWithin(bh.id, targetTime);
//Create a Results object to hold 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
//(an extra step added to allow this interface to also support bulkification)
List<Results> responseWrapper = new List<Results> ();
responseWrapper.add(response);
return responseWrapper;
}
//
global class Requests {
@InvocableVariable(label = '' description = 'Test Description' required = false)
public List<SObject> inputCollection;
}
//
global class Results {
@InvocableVariable(label = 'Within Business Hours' description = '' required = true)
public Boolean outputMember;
}
}
try below:
Class edited:
Test class:
regards,
Omar
All Answers
Hi Nicolai,
try the code below, and use the variable "inputCreatedDateTime" in the flow to set the target date.
regards,
Omar
Omar,
You sir are incredible!! That worked, thank you so much!!!
I created a boolean veriable in the flow to assign the outputMember and used a decision element that says if the boolean is true, do nothing, else create the record.
Hi Omar,
I am attempting to write my test class but something is obviously not clicking.
This is as far as I got. Any help would be greatly appreciated.
@isTest
private class CDiswithinBusinessHoursTest {
private static testMethod void test() {
Case c = new Case ();
Datetime recordCD = c.CreatedDate;
insert c;
List<Datetime> requestList = new List<Datetime>();
requestList.add(recordCD);
Test.startTest();
CreatedDateWithinBusinessHours.execute(requestList);
Test.stopTest();
System.assert();
}
}
try below:
Class edited:
Test class:
regards,
Omar