---
schema_version: "1"
id: 8ae445af41261652556151e8e5705edf9bf75a9067979b6bf85a1b55e29b1c05
title: Create a Case When a Messaging Session Starts Outside of Your Business Hours
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/service/264-0-0/miaw_create_case_outside_business_hours.md
canonical_url: https://help.salesforce.com/s/articleView?id=service.miaw_create_case_outside_business_hours.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: c3e5ef47bf77ef08a58afcd29cfe5d9133bdfc9871df36f823e2caaf8f5713ef
description: 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.
products:
  - Service Cloud
  - Digital Engagement
release: "264"
---
# 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

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition">
        <colgroup>
          <col style="width:50%">
          <col style="width:50%">
        </colgroup>
        <tbody>
          <tr class="">
            <td class="slds-cell-wrap" colspan="2" style="vertical-align:top;"><a href="https://help.salesforce.com/s/articleView?id=service.messaging_editions.htm&amp;type=5&amp;language=en_US" target="_blank">View supported editions.</a></td>

          </tr>
          <tr class="">
            <td class="slds-cell-wrap" style="vertical-align:top;">
              <a name="miaw_create_case_outside_business_hours__d921e335"><!-- --></a><span style="vertical-align:text-bottom;display:inline-block;"><img style="margin-bottom:0;" id="miaw_create_case_outside_business_hours__d921e335" src="https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-service-support_channels-264-0-0-production-enus/f3c4b3d2-ff86-4f7c-9fe2-3d8b23d35109/resources-global/_corporate/images/checkmark.png" alt="Checkmark"></span>
              <strong>This article applies to:</strong>
            </td>

            <td class="slds-cell-wrap" style="vertical-align:top;">Enhanced In-App Chat and Enhanced Web Chat channels</td>

          </tr>
          <tr class="">
            <td class="slds-cell-wrap" style="vertical-align:top;">
              <span style="vertical-align:text-bottom;display:inline-block;"><img style="margin-bottom:0;" src="https://sf-zdocs-cdn-prod.zoominsoftware.com/tdta-service-support_channels-264-0-0-production-enus/f3c4b3d2-ff86-4f7c-9fe2-3d8b23d35109/resources-global/_corporate/images/crossmark.png" alt="X icon"></span>
              <strong>This article doesn’t apply to:</strong>
            </td>

            <td class="slds-cell-wrap" style="vertical-align:top;">Enhanced WhatsApp, Enhanced Facebook Messenger, Standard and Enhanced SMS,
              Enhanced Apple Messages for Business, Enhanced LINE, and Bring Your Own
              Channel</td>

          </tr>
        </tbody>
      </table> <table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small permission">
        <colgroup>
          <col style="width:50%">
          <col style="width:50%">
        </colgroup>
        <thead class="slds-text-title_caps ">
          <tr class="">
            <th scope="colgroup" class="slds-cell-wrap" colspan="2" style="text-align:center;vertical-align:top;" id="d65245e98">User Permissions Needed</th>
          </tr>
        </thead>
        <tbody>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d65245e98 ">To create business hours:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d65245e98 ">Manage Business Hours Holidays</td>

          </tr>
          <tr class="">
            <td data-label="User Permissions Needed" class="slds-cell-wrap" style="vertical-align:top;" headers="d65245e98 ">To add business hours to your deployment:</td>

            <td class="slds-cell-wrap" style="vertical-align:top;" headers="d65245e98 ">Customize Application AND Modify Metadata Through Metadata API Functions</td>

          </tr>
        </tbody>
      </table>

## 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.](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_class.htm)

```
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](https://help.salesforce.com/s/articleView?id=service.customize_supporthours.htm&type=5&language=en_US). 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.

1. [Create a new flow](https://help.salesforce.com/s/articleView?id=platform.flow_build.htm&type=5&language=en_US). Select **Autolaunched Flow (No Trigger)**as the flow type.
2. Create a new flow Resource:
    1. In the Resource Type field, select **Variable**.
    2. In the API Name field, enter CaseNumber.
    3. In the Data Type field, select **Text**.
    4. In the Data Type field, select **Text**.
    5. Click **Done**.
3. Create a second flow Resource:
    1. In the Resource Type field, select **Variable**.
    2. In the API Name field, enter Subject.
    3. In the Data Type field, select **Text**.
    4. Select **Available for input**.
    5. Click **Done**.
4. 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.
    1. In the How to Set the Record Fields dropdown, select **Manually**.
    2. In the Object dropdown, select **Case**.
    3. Under Set Field Values for the Case, select **Subject** in the Field dropdown and enter Einstein Bot Case in the Value dropdown.
    4. Click **Done**.
5. 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.

1. 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.
    1. In the Action Type field, select **Apex**.
    2. In the Action Name field, select the apex class you created in step 1.
    3. Verify that the Input field displays **Name (string)**.
    4. In the Source field, select **Value**.
    5. In the Custom Value, enter the name of your business hours record.
    6. Verify that the Output field displays **Output (Boolean)**.
    7. Click inside the Variable Name field, and select **+New Boolean Variable**.
    8. In the Type field, select **Custom**.
    9. In the Name field, enter IsWithinBusinessHours. To give it the same API name, click inside the API Name field.
    10. Verify that the Data Type field displays **Boolean**.
    11. Save your changes.
    12. Save your changes in the Bot Builder.
2. 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.
    1. Click the **+** icon to the right of the search field in the Bot Builder, and select **New Dialogue**.
    2. Name your dialog, give it a description, and optionally, assign it to a dialog group.
    3. Add a Flow component to the dialogue.
    4. Verify that the Action Type field displays **Flow**.
    5. In the Action Name field, select the flow that you created previously.
    6. Verify that the Input field displays **Subject (string)**.
    7. In the Source field, select **Value**.
    8. In the Custom Value field, enter **Einstein Bots Case**.
    9. Verify that the Output field displays **CaseNumber (string)**.
    10. In the corresponding Variable Name field, select **\+ New Text Variable**.
    11. In the Type field, select **Custom**.
    12. In the Name field, enter Case Number. To give the text variable a similar API name, click inside the API field.
    13. Verify that the Data Type field displays **Text**.
    14. Save your changes.
    15. Save your changes to the Bot Builder.
3. 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.
    1. Click **+Add Condition**.
    2. In the Variable Name field, select **IsWithinBusinessHours (boolean).**
    3. In the Operator field, select **Is False**.
    4. In the Rule Action field, select **Redirect to Dialogue**.
    5. In the Redirect Type field, select **Dialogue**.
    6. In the Dialogue Case field, select the dialogue that you created in step 4b.
4. Save your changes in the bot builder.
5. Activate your bot.