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 

Business Hours Invocable Action

Hi,

 

This is my first question and I'm completely new to Apex.

I'm essentially trying to write am invocable class that I can use in a flow. It should check now() against the default business hours and return a boolean as an output variable that I can then use in a decision element. 

I think I found the piece on writing the query for business hours but I'm not sure how to turn that into an invocable action that I can use in a flow.

Any help would be greatly appreciated!!

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

The Apex-Code should be like this: 
public with sharing class GetFirstFromCollection {
	    @InvocableMethod(label = 'Get business hours' description = 'Get 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];

		// Create Datetime on May 28, 2013 at 1:06:08 AM in the local timezone.
		Datetime targetTime = Datetime.newInstance(2013, 5, 28, 1, 6, 8);

		// Find whether the time is within the default business hours
		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;
	}


	public class Requests {
		@InvocableVariable(label = 'Records for Input' description = 'yourDescription' required = false)
		public List<SObject> inputCollection;
	}

	public class Results {
		@InvocableVariable(label = 'Records for Output' description = 'yourDescription' required = true)
		public Boolean outputMember;
	}
}

Maybe you should change the name of the class and variable to be meaningful. 

In the Flow you add an Apex Action then you select the invocabel method "Get business hours". After that you can use the outputMember in the flow!

Regards,
Omar 

All Answers

Omar Rajab 94Omar Rajab 94
Hi Nicolai, 

Please see the example in the refference below, it should guide you to implement you requirements: 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm#:~:text=If%20a%20flow%20invokes%20Apex,values%20and%20support%20describe%20calls.

Let me know if anything is not clear to you, and mark as the best answer if this help you! 

Regards 
Omar 
Nicolai NonnenbroichNicolai Nonnenbroich

Hi Omar,

 

Thank you for the link! This is one of the resources I used as well as this one:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_businesshours.htm

// Get the default business hours
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
 
// Create Datetime on May 28, 2013 at 1:06:08 AM in the local timezone.
Datetime targetTime = Datetime.newInstance(2013, 5, 28, 1, 6, 8);
 
// Find whether the time is within the default business hours
Boolean isWithin= BusinessHours.isWithin(bh.id, targetTime);

 

Now it's just about putting the pieces together. Would you happen to have some pointers for working this piece of code into my invocable class?

Omar Rajab 94Omar Rajab 94
Hi Nicolai, 

The Apex-Code should be like this: 
public with sharing class GetFirstFromCollection {
	    @InvocableMethod(label = 'Get business hours' description = 'Get 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];

		// Create Datetime on May 28, 2013 at 1:06:08 AM in the local timezone.
		Datetime targetTime = Datetime.newInstance(2013, 5, 28, 1, 6, 8);

		// Find whether the time is within the default business hours
		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;
	}


	public class Requests {
		@InvocableVariable(label = 'Records for Input' description = 'yourDescription' required = false)
		public List<SObject> inputCollection;
	}

	public class Results {
		@InvocableVariable(label = 'Records for Output' description = 'yourDescription' required = true)
		public Boolean outputMember;
	}
}

Maybe you should change the name of the class and variable to be meaningful. 

In the Flow you add an Apex Action then you select the invocabel method "Get business hours". After that you can use the outputMember in the flow!

Regards,
Omar 
This was selected as the best answer
Nicolai NonnenbroichNicolai Nonnenbroich
Thanks so much! Would you care to explain the need for a custom object? Is this because I can't access fields for an invocable class?
Omar Rajab 94Omar Rajab 94
Which custom obejct you mean?
Nicolai NonnenbroichNicolai Nonnenbroich

Sorry, I most likely read this wrong. I was referring to this section:

//Create a Results object to hold the return values Results response = new Results();

//Add the return values to the Results object response.outputMember = isWithin;

 

Omar Rajab 94Omar Rajab 94

yes you are right:
"A list of a primitive data type or a list of lists of a primitive data type – the generic Object type is not supported."
Please refer to the link i sent you before, then go to the section "InvocableMethod Considerations".you will find the types of input and return supported by invocable method!
 

Nicolai NonnenbroichNicolai Nonnenbroich
Thank you! I really appreciate your help and your patience with my silly questions!
umaair liooumaair lioo
Business Hours Invocable Action is the title that you will like to know about for educational as well as for professional reasons, so just check this out and we hope http://signalscv.com/2022/01/how-to-find-the-best-essay-writing-service-in-uk/ can be a good source that one can recommend to others who find similar data.