You are here:
Create a Case When a Messaging Session Starts Outside of Your Business Hours
Let end users submit a case when trying to message outside of business hours. Use an Apex class and a bot to run a business hours check, then create the case from a flow.
Required Editions
| View supported editions. | |
This article applies to:
|
Enhanced In-App Chat and Enhanced Web Chat channels |
This article doesn’t apply to:
|
Enhanced WhatsApp, Standard and Enhanced Facebook Messenger, Standard and Enhanced SMS, Enhanced Apple Messages for Business, Enhanced LINE, and Bring Your Own Channel |
| User Permissions Needed | |
|---|---|
| To create business hours: | Manage Business Hours Holidays |
| To add business hours to your deployment: | Customize Application AND Modify Metadata Through Metadata API Functions |
Create an Apex Class
Create a public Apex class that checks if an end user is attempting to message outside of your business hours.
Add this code snippet to your Apex class.
public class CheckWithinBusinessHours {
@InvocableMethod(label = 'Check Within Business Hours')
public static List<Boolean> makeGetCallout(List<String> Name) {
String BHName= String.valueOf(+Name[0]);
// Get Business hours based on name, input from bot
BusinessHours bh = [SELECT Id FROM BusinessHours WHERE name= :BHName];
// Create Datetime with current date/time in local time zone
Datetime targetTime = Datetime.now();
// Find whether the time is within business hours
Boolean isWithin= BusinessHours.isWithin(bh.id, targetTime);
return new List<Boolean>{isWithin};
}}
Create Business Hours
Create a business hours record for your Apex class to reference.
Create a set of business hours. For the purposes
of this recipe, don’t associate a business hours record with your Enhanced Chat
deployment.
Create a Flow
Create an Auto-Launched flow that creates a case when a customer messages outside of business hours.
- Create a new flow. Select Autolaunched Flow (No Trigger)as the flow type.
-
Create a new flow Resource:
- In the Resource Type field, select Variable.
- In the API Name field, enter CaseNumber.
- In the Data Type field, select Text.
- In the Data Type field, select Text.
- Click Done.
-
Create a second flow Resource:
- In the Resource Type field, select Variable.
- In the API Name field, enter Subject.
- In the Data Type field, select Text.
- Select Available for input.
- Click Done.
-
A CaseId from Create_Case variable is generated automatically when the Create Records
element is added to your flow.To create a Case record and return the Case Number to the user
in the bot, add a Create Records element.
- In the How to Set the Record Fields dropdown, select Manually.
- In the Object dropdown, select Case.
- Under Set Field Values for the Case, select Subject in the Field dropdown and enter Einstein Bot Case in the Value dropdown.
- Click Done.
- Save and activate your flow.
Add the Apex Class to Your Bot
Update your bot by adding the Apex class. While this Apex action could be placed and executed anywhere in your bot, this example invokes the class in the Transfer to Agent dialogue.
-
Add an Apex action component to the Transfer to Agent dialogue for your bot. This
component lets you associate the Apex class that you created in step 1 to your bot.
- In the Action Type field, select Apex.
- In the Action Name field, select the apex class you created in step 1.
- Verify that the Input field displays Name (string).
- In the Source field, select Value.
- In the Custom Value, enter the name of your business hours record.
- Verify that the Output field displays Output (Boolean).
- Click inside the Variable Name field, and select +New Boolean Variable.
- In the Type field, select Custom.
- In the Name field, enter IsWithinBusinessHours. To give it the same API name, click inside the API Name field.
- Verify that the Data Type field displays Boolean.
- Save your changes.
- Save your changes in the Bot Builder.
-
Add a dialogue that creates a case if the Apex class returns false when checking to see if
the messaging session is initiated outside of business hours.
- Click the + icon to the right of the search field in the Bot Builder, and select New Dialogue.
- Name your dialog, give it a description, and optionally, assign it to a dialog group.
- Add a Flow component to the dialogue.
- Verify that the Action Type field displays Flow.
- In the Action Name field, select the flow that you created previously.
- Verify that the Input field displays Subject (string).
- In the Source field, select Value.
- In the Custom Value field, enter Einstein Bots Case.
- Verify that the Output field displays CaseNumber (string).
- In the corresponding Variable Name field, select + New Text Variable.
- In the Type field, select Custom.
- In the Name field, enter Case Number. To give the text variable a similar API name, click inside the API field.
- Verify that the Data Type field displays Text.
- Save your changes.
- Save your changes to the Bot Builder.
-
Return to the Transfer to Agent dialogue and add a Redirect to Dialogue component.
This component redirects to the Create a Case dialogue when an end user tries to chat
outside of business hours.
- Click +Add Condition.
- In the Variable Name field, select IsWithinBusinessHours (boolean).
- In the Operator field, select Is False.
- In the Rule Action field, select Redirect to Dialogue.
- In the Redirect Type field, select Dialogue.
- In the Dialogue Case field, select the dialogue that you created in step 4b.
- Save your changes in the bot builder.
- Activate your bot.
Did this article solve your issue?
Let us know so we can improve!



