---
schema_version: "1"
id: cae718dc9acec663e6cda3ab5c1981a558f74819486caa55025e39397ff207e4
title: Create Apex Handlers for Slack Apps
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/salesforce-slack-sdk/apex-slack-sdk/guides/apex_ref/apex_ref_dispatchers.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-slack-sdk/guide/apex_ref_dispatchers.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: c5fcc7b1c90603f6058a88669c10a9578614c1b47f636e588eb3a80ea3deffb5
products:
  - Apex SDK for Slack (Beta)
---
# Create Apex Handlers for Slack Apps

Use these Apex classes to create handlers for Slack actions, commands, shortcuts, and events.

## ActionDispatcher Class

The `Slack.ActionDispatcher` class enables you to invoke an action handler such as in response to a form submission.

**Usage**

Extend the `ActionDispatcher` class and override the `invoke()` method.

```java
public class MyActionDispatcher extends Slack.ActionDispatcher {

    public override Slack.ActionHandler invoke(Map<String, Object> parameters, Slack.RequestContext context) {
        return Slack.ActionHandler.modal(new Handler(context));
    }
}
```

**Examples**

This example uses `ActionDispatcher` to handle a form submission.

```java
public class ExampleFormSubmissionDispatcher extends Slack.ActionDispatcher {

    public override Slack.ActionHandler invoke(Map<String, Object> parameters, Slack.RequestContext context) {
        return Slack.ActionHandler.modal(new Handler(context));
    }

    public class Handler implements Slack.ModalHandler {
        Slack.RequestContext context;
        public Handler (Slack.RequestContext context){
            this.context = context;
        }
        public Slack.ModalView call () {
            Map <String, Object> formData = context.getFormData();
            Slack.ViewReference viewReference = Slack.View.message_modal.get();
            viewReference.setParameter('title', 'Form Field Value');
            viewReference.setParameter('message', (String) formData.get('FormFieldName'));
            Slack.ModalView modalView = new Slack.ModalView.builder()
                .viewReference(viewReference)
                .build();
            return modalView;
        }
    }
}
```

This example uses `ActionDispatcher` to display a view.

```java
public class createOpportunity extends Slack.ActionDispatcher {
    public override Slack.ActionHandler invoke(Map<String, Object> parameters, Slack.RequestContext context) {
        return Slack.ActionHandler.modal(new Handler(parameters, context));
    }
    public class Handler implements Slack.ModalHandler {
        Map<String, Object> parameters;
        Slack.RequestContext context;
        public Handler (Map<String, Object> parameters, Slack.RequestContext context){
            this.parameters = parameters;
            this.context = context;
        }
        public Slack.ModalView call () {
            Slack.ViewReference viewReference = Slack.View.create_opportunity.get();
            viewReference.setParameter('title', 'Record Created');
            viewReference.setParameter('message', 'Your opportunity was created');
            Slack.ModalView modalView = new Slack.ModalView.builder()
                .viewReference(viewReference)
                .build();
            return modalView;
        }
    }
```

You can close the modal and optionally clear the view stack by using `clearModal()`. To close the modal _without_ clearing the view stack, use [`ack(handler)`](#actionhandler-methods) instead.

```java
public class ExampleViewSubmissionDispatcher extends Slack.ActionDispatcher {

    public override Slack.ActionHandler invoke(Map<String, Object> parameters, Slack.RequestContext context) {
        // Tell the framework how to respond first

        // Close the modal and clear the whole stack
        return Slack.ActionHandler.clearModal(new Handler(parameters, context));
    }

    public class Handler implements Slack.ModalHandler {

        Map<String, Object> parameters;
        Slack.RequestContext context;

        public Handler (Map<String, Object> parameters, Slack.RequestContext context) {
            this.parameters = parameters;
            this.context = context;
        }

        public void run () {
            // Process the request (e.g. context.getFormData())
        }
    }
}
```

**tip:** For an example of invoking an action handler on the App Home page, such as with `SlashCommandDispatcher`, see [ChatPostMessageRequest](https://developer.salesforce.com/docs/platform/salesforce-slack-sdk/guide/apex_ref_chat.md#chatpostmessagerequest-class).

### ActionDispatcher Methods

These methods are for `ActionDispatcher`.

#### allowUnauthenticatedUsers()

**Signature**

```java
public Boolean allowUnauthenticatedUsers()
```

**Return Value**

Type: Boolean

#### invoke(parameters, context)

Invokes the action handler.

**Signature**

```java
public Slack.ActionHandler invoke(
  Map<String, Object> parameters,
  Slack.RequestContext context
);
```

**Parameters**

_parameters_

Type: Map&lt;String, Object&gt;

The action parameters or other parameters of `EventParameters`, `ShortcutParameters`, or `SlashCommandParameters` type.

_context_

Type: Slack.RequestContext

Describes the context in which a Slack action is invoked.

**Return Value**

Type: Slack.ActionHandler

**Usage**

To access the Slack context, use the `invoke()` method. This method is passed in by an instance of `RequestContext`. It exposes information about the source of the action in Slack, performs custom logic, and obtains the correct Slack client to respond to Slack based on the request.

Each action type receives different information in its `RequestContext`.

---

## ActionHandler Class

The `Slack.ActionHandler` class provides methods that control how Slack actions are handled. The handler processes a single request.

**Usage**

With the `ActionHandler` methods, you can [work with modals](https://api.slack.com/surfaces/modals/using). By responding to Slack actions with a modal you can display dynamic and interactive information. Before opening a modal, compose a view object to define the layout of the initial view.

A modal can hold up to three views simultaneously in a view stack. Only one view is visible each time. But the view stack can retain previous views and return to them with their prior state in place.
An app can push new views onto a modal's view stack or update an existing view within that stack, including the currently visible view.

To use this Apex class, specify the `Slack` namespace when creating an instance of this class. Override the `invoke()` method returning a `Slack.ActionHandler`.

**Examples**

This example pushes a modal onto the modal view stack.

```java
public class createRecord extends Slack.ActionDispatcher {

    public override Slack.ActionHandler invoke(Map<String, Object> parameters, Slack.RequestContext context) {
        // the slack action handler is returned immediately
        return Slack.ActionHandler.pushModal(new Handler(parameters, context));
    }

    public class Handler implements Slack.ModalHandler {

        Map<String, Object> parameters;
        Slack.RequestContext context;

        public Handler (Map<String, Object> parameters, Slack.RequestContext context) {
            this.parameters = parameters;
            this.context = context;
        }

        public Slack.ModalView call () {
            Slack.ViewReference viewReference = Slack.View.message_modal.get();
            viewReference.setParameter('title', 'Title');
            viewReference.setParameter('message', 'Message');
            Slack.ModalView modalView = new Slack.ModalView.builder()
                .viewReference(viewReference)
                .build();
            return modalView;
        }

    }
}
```

The `message_modal.view` file looks like this.

```yaml
description: "This is a simple component that displays a message in a modal"
schema:
  properties:
    title:
      type: string
      required: true
    message:
      type: string
      required: true
components:
  - definition: modal
    properties:
      title: "{!view.properties.title}"
    components:
      - definition: section
        properties:
          text: "{!view.properties.message}"
```

### ActionHandler Methods

The following are methods for `ActionHandler`.

#### ack(handler)

Acknowledge the payload back to Slack to avoid the 3-second timeout. After running this method, you can perform Slack Client API calls such as to post messages and publish to app home. For example, you can listen to the `app_home_opened` event and publish a new view to the user’s Home tab. `ack()` takes care of responding back to Slack immediately to avoid the 3-second timeout.

We don’t recommend using `ack()` for displaying modals subject to the `trigger_id` timeouts because doing so can cause your code to fail intermittently, depending on the environment.

To create, update, or push a modal view, use `pushModal()`, `modal()`, or `updateModal()` instead. These methods ack back to Slack within 3 seconds so that the `trigger_id` from Slack doesn't expire using a staging modal. Using these methods, your `Handler` must return a `ModalView` for Slack to display. See [Create an Apex Handler](https://developer.salesforce.com/docs/platform/salesforce-slack-sdk/build/actions_handlers_apex.md).

**Signature**

```java
public static Slack.ActionHandler ack(Slack.RunnableHandler handler)
```

**Parameters**

_handler_

Type: Slack.RunnableHandler

The handler that clears the modal from the view stack.

**Return Value**

Type: Slack.ActionHandler

#### clearModal(handler)

Clears the full stack of modals. By default, Slack pops one modal off the top of the stack.

**Signature**

```java
public static Slack.ActionHandler clearModal(RunnableHandler handler)
```

**Parameters**

_handler_

Type: RunnableHandler

The handler that clears the modal from the view stack.

**Return Value**

Type: Slack.ActionHandler

#### modal(handler)

Displays a **Loading** modal with automatic staging to satisfy the 3-second timeout rule.

**Signature**

```java
public static Slack.ActionHandler modal(Slack.ModalHandler handler)
```

**Parameters**

_handler_

Type: Slack.ModalHandler

The handler for the modal.

**Return Value**

Type: Slack.ActionHandler

#### Window(handler, stagedModalTitle)

Displays a **Loading** modal with a title. The modal has automatic staging to satisfy the 3-second timeout rule.

**Signature**

```java
public static Slack.ActionHandler modal(
  Slack.ModalHandler handler,
  String stagedModalTitle
)
```

**Parameters**

_handler_

Type: Slack.ModalHandler

The handler for the modal.

_stagedModalTitle_

Type: String

The title to display on the modal in Slack.

**Return Value**

Type: Slack.ActionHandler

#### pushModal(handler)

Pushes another modal to the view stack with automatic staging to satisfy the 3-second timeout.

**Signature**

```java
public static Slack.ActionHandler pushModal(Slack.ModalHandler handler)
```

**Parameters**

_handler_

Type: Slack.ModalHandler

The handler that pushes the modal to the view stack.

**Return Value**

Type: Slack.ActionHandler

#### pushModal(handler, stagedModalTitle)

Push another modal to the view stack with a different title.

**Signature**

```java
public static Slack.ActionHandler pushModal(
  Slack.ModalHandler handler,
  String stagedModalTitle
)
```

**Parameters**

_handler_

Type: Slack.ModalHandler

The handler that pushes the modal to the view stack.

_stagedModalTitle_

Type: String

The title to display on the modal in Slack.

**Return Value**

Type: Slack.ActionHandler

#### updateModal(handler)

Updates a modal. This update can happen if the view is visible within the modals's view stack.

**Signature**

```java
public static Slack.ActionHandler updateModal(Slack.ModalHandler handler)
```

**Parameters**

_handler_

Type: Slack.ModalHandler

The handler that updates the modals.

**Return Value**

Type: Slack.ActionHandler

#### updateModal(handler, stagedModalTitle)

Updates a modals with a different title. This update can happen whether the view is visible within the modal's view stack.

**Signature**

```java
public static Slack.ActionHandler updateModal(
  Slack.ModalHandler handler,
  String stagedModalTitle
)
```

**Parameters**

_handler_

Type: Slack.ModalHandler

The handler that updates the modal.

`stagedModalTitle`

Type: String

The title to display on the modal in Slack.

**Return Value**

Type: Slack.ActionHandler

---

## ActionPayload Class

Contains the name and values associated with the block element that triggered the action.

**Usage**

Use the `ActionPayload` class to register an event, such as an `onchange` or `onclick` event, and associate the event to an action. You can retrieve the value from the payload to display it in the view or use the value to perform a query and so on.

Specify your action payload in the `run()` callback method.

```java
// Set the context using Slack.RequestContext
Slack.ActionPayload actionPayload = context.getActionPayload();
    if (actionPayload != null) {
        String name = actionPayload.getName();
        // do something with the payload
    }
```

See [ChatPostMessageRequest](https://developer.salesforce.com/docs/platform/salesforce-slack-sdk/guide/apex_ref_chat.md#chatpostmessagerequest-class) for an example that retrieves the value of the payload and publish it to a view.

### ActionPayload Methods

The following are methods for `ActionPayload`.

#### getName()

Returns the name of the block element.

**Signature**

```java
public String getName()
```

**Return Value**

Type: String

#### getType()

Returns the type of the block element.

**Signature**

```java
public String getType()
```

**Return Value**

Type: String

#### getValue()

Returns the value of the block element.

**Signature**

```java
public Object getValue()
```

**Return Value**

Type: Object

---

## ActionPayload.Builder Class

Contains methods to build an instance of the `Slack.ActionPayload` class.

A Builder object is obtained by invoking one of the `ActionPayload.Builder` methods defined by the `ActionPayload` class.

### ActionPayload.Builder Methods

The following are methods for `ActionPayload.Builder`.

#### build()

Returns an instance of the `Slack.ActionPayload` object.

**Signature**

```java
public Slack.ActionPayload build()
```

**Return Value**

Type: Slack.ActionPayload

#### name(name)

**Signature**

```java
public Slack.ActionPayload.Builder name(String name)
```

**Parameters**

_type_

Type: String

**Return Value**

Type: Slack.ActionPayload.Builder

#### type(type)

**Signature**

```java
public Slack.ActionPayload.Builder type(String type)
```

**Parameters**

_type_

Type: String

**Return Value**

Type: Slack.ActionPayload.Builder

#### value(value)

**Signature**

```java
public Slack.ActionPayload.Builder value(Object value)
```

**Parameters**

_value_

Type: Object

**Return Value**

Type: Slack.ActionPayload.Builder
