# Create an Apex Handler

You can create an Apex class either via the [Developer Console](https://help.salesforce.com/s/articleView?id=platform.code_dev_console_opening.htm\&type=5) in your org or by creating these files in the `force-app/main/default/classes` directory in your SFDX project.

- `YourClassName.cls`: The Apex class
- `YourClassName.cls-meta.xml`: Apex class metadata

For more information, see [Create an Apex Class](https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_develop_create_apex.htm).

Extend the dispatcher based on the type of operation that you're handling.

```java
// Action
public class MyActionDispatcher extends Slack.ActionDispatcher {}

// Event
public class MyEventDispatcher extends Slack.EventDispatcher {}

// Shortcut
public class MyShortcutDispatcher extends Slack.ShortcutDispatcher {}

// Slash commands
public class MySlashCommandDispatcher extends Slack.SlashCommandDispatcher {}
```

## Override the `invoke()` Method

1. Pass in your Slack parameters and context and return a `Slack.ActionHandler` instance.

   ```java
   public override Slack.ActionHandler invoke(Slack.SlashCommandParameters parameters, Slack.RequestContext context) {
       return Slack.ActionHandler.modal(new Handler(parameters, context));
   }
   ```

   [`Slack.ActionHandler`](/docs/platform/salesforce-slack-sdk/guide/apex_ref_dispatchers.md#actionhandler-class) has methods to help you structure your handler and enable you to work with modals.

2. Create a handler that is passed as an argument to `Slack.ActionHandler` by using the [`ModalHandler` interface](/docs/platform/salesforce-slack-sdk/guide/apex_ref_modal.md).

:::tip
Handlers can implement [`Slack.ModalHandler`](/docs/platform/salesforce-slack-sdk/guide/apex_ref_modal.md) or [`Slack.RunnableHandler`](/docs/platform/salesforce-slack-sdk/guide/apex_ref_runnablehandler.md).
:::

```java
public class Handler implements Slack.ModalHandler {
    public Handler (Slack.SlashCommandParameters parameters,
        Slack.RequestContext context){
    }
}
```

1. Implement the required methods for the interface by using `call()`.

   ```java
   public Slack.ModalView call() {
       Slack.ModalView modalView = new Slack.ModalView.builder().build();
       return modalView;
   }
   ```

## Examples

Use these examples to implement your Apex handlers.

### Basic Slash Command

This example creates a handler for a slash command, returns the `ack()` method to avoid the 3-second timeout, and runs a callback method.

```java
public class MySlashCommandDispatcher extends Slack.SlashCommandDispatcher {

    public override Slack.ActionHandler invoke(Slack.SlashCommandParameters parameters, Slack.RequestContext context) {
        return Slack.ActionHandler.ack(new Handler());
    }

    public class Handler implements Slack.RunnableHandler {
        public void run () {
            System.debug('Ack Callback');
        }
    }
}
```

### Slash Command Returning a Modal View

This example handles a slash command and returns a view definition that contains a modal.

```java
public class MySlashCommandDispatcher extends Slack.SlashCommandDispatcher {

    public override Slack.ActionHandler invoke(Slack.SlashCommandParameters parameters, Slack.RequestContext context) {
        return Slack.ActionHandler.modal(new Handler(parameters, context), 'my custom modal title');
    }

    public class Handler implements Slack.ModalHandler {

        Slack.SlashCommandParameters parameters;
        Slack.RequestContext context;

        public Handler (Slack.SlashCommandParameters parameters, Slack.RequestContext context){
            this.parameters = parameters;
            this.context = context;
        }

        public Slack.ModalView call () {

            Slack.ViewReference viewReference = Slack.View.example_view.get();
            viewReference.setParameter('title', 'Apex Modal!');
            viewReference.setParameter('message', context.getAppId() + ' ' + parameters.getText());

            Slack.ModalView modalView = new Slack.ModalView.builder()
                .viewReference(viewReference)
                .build();

            return modalView;

        }
    }

}
```

### Global Shortcut Acknowledging Request

This example handles a global shortcut by acknowledging the request.

```java
public class ExampleShortcutDispatcher extends Slack.ShortcutDispatcher {

    public override Slack.ActionHandler invoke(Slack.ShortcutParameters parameters, Slack.RequestContext context) {
        return Slack.ActionHandler.ack(new Handler(parameters, context));
    }

    public class Handler implements Slack.RunnableHandler {

        Slack.ShortcutParameters parameters;
        Slack.RequestContext context;

        public Handler (Slack.ShortcutParameters parameters, Slack.RequestContext context){
            this.parameters = parameters;
            this.context = context;
        }

        public void run () {
            System.debug('Ack Callback');
        }

    }
}
```

### Shortcut Returning a Modal View

```java
public class CreateRecordShortcutDispatcher extends Slack.ShortcutDispatcher {

    public override Slack.ActionHandler invoke(Slack.ShortcutParameters parameters, Slack.RequestContext context) {
        return Slack.ActionHandler.modal(new Handler(parameters, context), 'Create Record');
    }

    public class Handler implements Slack.ModalHandler {

        Slack.ShortcutParameters parameters;
        Slack.RequestContext context;

        public Handler (Slack.ShortcutParameters parameters, Slack.RequestContext context){
            this.parameters = parameters;
            this.context = context;
        }

        public Slack.ModalView call () {
            Slack.ViewReference viewReference = Slack.View.create_record.get();
            Slack.ModalView modalView = new Slack.ModalView.builder()
                .viewReference(viewReference)
                .build();
            return modalView;
        }
    }
}
```
