function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nicolai NonnenbroichNicolai Nonnenbroich 

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;
    }
}

 

Best Answer chosen by Nicolai Nonnenbroich
Omar Rajab 94Omar Rajab 94
Hi Nicolai, 

try below: 

Class edited: 
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) {
           
            // 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 = requestList[0].inputCreatedDateTime;

            // 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);
			if(Test.isRunningTest()) {
			isWithin = true;
			}
            //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 = 'Created Date Time‘ description = 'Test Description' required = false)
       Datetime inputCreatedDateTime;
        
    }
    //
    global class Results {
        @InvocableVariable(label = 'Within Business Hours' description = '' required = true)
        public Boolean outputMember;
    }
}


Test class: 
@isTest
private class CDiswithinBusinessHoursTest {

	private static testMethod void test() {

		Datetime recordCD = Datetime.now();
		List<CreatedDateWithinBusinessHours.Requests> reqList = new List<CreatedDateWithinBusinessHours.Requests> ();
		CreatedDateWithinBusinessHours.Requests req = new CreatedDateWithinBusinessHours.Requests();
		req.inputCreatedDateTime = recordCD;
		reqList.add(req);
		List<CreatedDateWithinBusinessHours.Results> resList = new List<CreatedDateWithinBusinessHours.Results> ();
		Test.startTest();

		resList = CreatedDateWithinBusinessHours.execute(reqList);
		Test.stopTest();
		System.assertEquals(resList[0].outputMember, true);

	}
}

regards,
Omar

All Answers

Omar Rajab 94Omar Rajab 94

Hi Nicolai,
 

try the code below, and use the variable "inputCreatedDateTime" in the flow to set the target date.

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) {
           
            // 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 = requestList[0].inputCreatedDateTime;

            // 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 = 'Created Date Time‘ description = 'Test Description' required = false)
       Datetime inputCreatedDateTime;
        
    }
    //
    global class Results {
        @InvocableVariable(label = 'Within Business Hours' description = '' required = true)
        public Boolean outputMember;
    }
}

 

regards,
Omar

Nicolai NonnenbroichNicolai Nonnenbroich

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. 

 

Nicolai NonnenbroichNicolai Nonnenbroich

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();
       
   }
}    

Omar Rajab 94Omar Rajab 94
Hi Nicolai, 

try below: 

Class edited: 
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) {
           
            // 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 = requestList[0].inputCreatedDateTime;

            // 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);
			if(Test.isRunningTest()) {
			isWithin = true;
			}
            //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 = 'Created Date Time‘ description = 'Test Description' required = false)
       Datetime inputCreatedDateTime;
        
    }
    //
    global class Results {
        @InvocableVariable(label = 'Within Business Hours' description = '' required = true)
        public Boolean outputMember;
    }
}


Test class: 
@isTest
private class CDiswithinBusinessHoursTest {

	private static testMethod void test() {

		Datetime recordCD = Datetime.now();
		List<CreatedDateWithinBusinessHours.Requests> reqList = new List<CreatedDateWithinBusinessHours.Requests> ();
		CreatedDateWithinBusinessHours.Requests req = new CreatedDateWithinBusinessHours.Requests();
		req.inputCreatedDateTime = recordCD;
		reqList.add(req);
		List<CreatedDateWithinBusinessHours.Results> resList = new List<CreatedDateWithinBusinessHours.Results> ();
		Test.startTest();

		resList = CreatedDateWithinBusinessHours.execute(reqList);
		Test.stopTest();
		System.assertEquals(resList[0].outputMember, true);

	}
}

regards,
Omar
This was selected as the best answer