Slack for Salesforce Developers | Salesforce Developers Blog

With Salesforce’s acquisition of Slack, Salesforce is realizing a major goal: to help people work from anywhere in this new all-digital world. Salesforce developers have the opportunity to be at the forefront of this world by using Slack to surface the information their customers need at the time they need it.

Developers can extend their apps and allow users to use Slack as their collaboration hub and take action on data from Salesforce and other business systems. If you are wondering what you can build with Slack and Salesforce together, and how to do it, this post is for you. We’ll explore how to build Slack apps integrated with Salesforce data.

You might think of Slack as a messaging app. However, what excites me as a Salesforce developer is the Slack platform, which lets you extend, expand, and automate your organization’s work.

What is a Slack app?

Slack allows developers to create custom experiences by using Slack APIs. These are called “Slack apps” and they have various components:

  • An app configuration (this configuration is done on api.slack.com) that defines how your app will communicate with Slack
  • A service that handles making API calls and can receive and route events; you can use the infrastructure of your choice to host this service
  • A Slack user interface, which could be in a Slack message, the App Home, or even a pop-up modal

You can write Slack apps just for your team or publish them to the Slack App Directory to allow others to install them. When building Slack apps, you can write code in your language of choice to integrate with various third-party services, including Salesforce.

Creating a Slack app

The first step in integrating Slack with Salesforce involves creating a Slack app. You can do this manually or using a manifest YML (see Slack API documentation). Using the manifest YML is the recommended way to start, and it makes it simpler to clone your production apps for testing needs or fixing bugs.

Note: you will need to sign in to your Slack account to create a Slack app. If you do not have Slack workspace yet, you can sign up for free.

Once you’ve created your Slack app, you can use the dashboard to view or configure:

  • App credentials that allow your apps to access Slack APIs. These credentials should be stored securely and protected.
  • A user interface to configure your Slack app scopes, which govern the app’s capabilities and permissions
  • Webhook URLs that allow you to connect to Slack via an HTTP POST to the URL
  • Configurations related to the app, such as:
    • Slash commands and shortcuts — you can set your app up to respond to user input using the lightning bolt button in the composer (a shortcut) or a text-based command beginning with a / (a slash command). Think of /giphy to search for a GIF or /polly to launch a poll.
    • URLs to subscribe to Slack Events API. Slack apps can subscribe to various activities in Slack such as channel created, channel archived, and many more event types.
    • App branding, thumbnail, and descriptions
    • App distribution — this is applicable if you are publishing your app to the Slack App Directory

What is a Slack bot?

A Slack bot is a unique user of your Slack app that can wait for your command, interpret your command, and perform actions, such as finding what you need.

All requests to Slack APIs have to be signed with a token. Slack has different types of tokens as discussed below:

    • Bot token — Bot tokens represent a bot associated with the app installed in a workspace. Bot tokens are tied to your app. Since acting independently allows your app to stay installed, even when an installing user is deactivated, using bot tokens is usually for the best.
    • User tokenUser tokens represent workspace members. They are issued for the user who installed the app and for users who authenticate the app. You can use these tokens to take action on behalf of users.
    • App-Level Tokens — App-level tokens represent your app across organizations, including installations by all individual users on all workspaces in a given organization.

The image below shows the dashboard where you will create and configure a Slack app.

Slack app creation and configuration.

Connecting to Slack from Salesforce

A typical pattern that developers need when integrating Slack and Salesforce is a method for sending messages (containing Salesforce data) from Salesforce upon an event or an action in Salesforce.

For example, let’s imagine that you want to build an integration that will automatically notify your marketing team in Slack upon every new lead creation in Salesforce.

The above use case can be solved using one of the approaches listed below:

  1. Using low code tools like Mulesoft Composer — you can learn more about this using this trailhead module
  2. Using external services and Salesforce Flows — Slack provides OpenAPI specs used to generate flow actions in Salesforce to invoke Slack Web APIs
  3. Using a Salesforce Flow or Trigger calling an Apex method that invokes Incoming Webhooks and various Slack Web APIs

All of the above approaches require first creating a named credential to authorize the Slack application to connect to Salesforce. See step-by-step instructions to configure named credentials and an auth provider.

A high-level solution implementation for the above use case is shown below.

Sending Slack messages from Salesforce upon lead creation.

Slack Workflow Builder

Another exciting low code tool from Slack is Workflow Builder, which lets you automate routine processes into workflows in your Slack workspace. You can invoke a Slack workflow from Salesforce by invoking the request webhook URL, which is auto-generated by Slack.

Connecting to Salesforce from Slack

Another typical pattern that developers need when building Slack apps integrated with Salesforce is the ability for the app to log into Salesforce, fetch data, and perform DML operations on data from the Slack interface.

For example, let’s imagine that your users need a Slack app that can be installed in a Slack workspace to perform the following functions:

  • Log in to Salesforce from Slack via a slash command (by typing /sf-login) in Slack. Upon command execution, users are sent to the Salesforce login page to use their Salesforce credentials to authenticate to Salesforce.
  • Find cases from Salesforce within Slack via a slash command (by typing /show-my-cases) in Slack.
  • Update the case status right from within Slack by opening a modal upon clicking on the case record.
  • Take action to update the case in Salesforce via a button in the modal interface.

Here is the high-level architecture for the Slack app to support the above use case.

Slack app connecting to Salesforce.

Slack gives developers the flexibility to choose their preferred infrastructure and language for building Slack apps. There are open source SDKs for Java, JavaScript, and Python known as Bolt, which are provided and maintained by Slack to make it easier to develop apps and comply with security standards.

As a Salesforce developer, I am more familiar with JavaScript, so I’d choose Slack’s JavaScript SDK. But you can choose the SDK you wish based on your comfort with the language. Now, if you are adventurous and want to learn a new language in the process of building your new Slack app, then go for it!

For hosting Slack apps, I prefer to use Heroku. The elastic runtime of Heroku, the availability of build packs for various open-source languages, and first-class developer experience make it ideal for hosting and scaling Slack apps.

For our use case above, the Slack app hosted on Heroku acts as a bridge (broker) between Salesforce and Slack and connects both systems. The important thing to observe in the above architecture diagram is that Salesforce is the single source of truth for all the data.

Authorization and authentication to Salesforce

To log in and obtain a Salesforce session securely, you will need to use a Connected App in Salesforce. You can configure the secrets in the environment variables of the Heroku app. If you are using Node.js, you can leverage an open-source library like JSForce to authenticate, query, and update records in Salesforce.

If you are fetching Salesforce data via the Heroku app as an integration user, you can leverage the server to server JWT flow to manage authentication. Using this flow, you can skip having to ask every user to go through the OAuth flow.

If you need to preserve Salesforce user context, then provide a slash command for users to go through an OAuth dance with Salesforce.

Authorization and authentication to Slack

Thanks to the Bolt SDK, most of the authentication and authorization is provided out of the box. However, one thing that the SDK does not include is database implementation to preserve tokens. You can use Heroku Postgres with encrypted database connections or any other equivalent Heroku add-on to implement this for your apps.

Bolt SDK

Slack apps have special requirements to keep the app secure and performant. Some of the important ones include:

The Bolt SDK provides these features out of the box, making this step easier for developers. Apart from these, the Bolt SDK also simplifies OAuth, provides methods for Web APIs and Events APIs, and much more.

Block Kit

Block Kit provides developers with UI components using a JSON data format. It is unique because you do not have to write HTML markup or CSS, unlike with web apps. If you are a back-end-focused developer, it cannot get easier than this!

Using the Block Kit UI Builder, developers, marketers, and designers can design and prototype message blocks. The Builder also can send the messages directly to your Slack workspace.

Below is sample JSON for the block UI message showing recent cases from Salesforce. To see this in action, simply copy this JSON and paste it into Block Kit UI Builder.

{
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "We found *5 new Cases* Assigned to you today"
            }
        },
        {
            "type": "divider"
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "*<https://cmpaura-dev-ed.lightning.force.com/lightning/r/Case/5003t00001Ah6vmAAB/view|Case - 00001002>*\n\n *Subject*: Seeking guidance on electrical wiring installation for GC5060"
            }
        },
        {
            "type": "context",
            "elements": [
                {
                    "type": "plain_text",
                    "emoji": true,
                    "text": "Contact: Stella Pavlova"
                }
            ]
        },
        {
            "type": "divider"
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "*<https://cmpaura-dev-ed.lightning.force.com/lightning/r/Case/5003t00001Ah6w0AAB/view|Case - 00001002>*\n\n *Subject*: Maintenance guidelines for generator unclear"
            }
        },
        {
            "type": "context",
            "elements": [
                {
                    "type": "plain_text",
                    "emoji": true,
                    "text": "Contact: Edna Frank"
                }
            ]
        },
        {
            "type": "divider"
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "*<https://cmpaura-dev-ed.lightning.force.com/lightning/r/Case/5003t00001Ah6w8AAB/view| Case - 00001024>*\n\n *Subject*: Design issue with mechanical rotor"
            }
        },
        {
            "type": "context",
            "elements": [
                {
                    "type": "plain_text",
                    "emoji": true,
                    "text": "Contact: Lauren Boyle"
                }
            ]
        },
        {
            "type": "divider"
        },
        {
            "type": "actions",
            "elements": [
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "emoji": true,
                        "text": "Next 2 Results"
                    },
                    "value": "click_me_123"
                }
            ]
        }
    ]
}

The JSON block produces a UI component, as shown below.

Recent cases from Salesforce in Slack with Block UI used to construct the message.

Slack App Directory

Like Salesforce AppExchange (where developers can distribute Salesforce apps), Slack enables developers to build and distribute their Slack apps via the Slack App Directory.

Slack developer tools

Slack provides a number of helpful developer tools. My personal favorite is the Slack Developer tools app. It’s a Slack app that can be installed in any workspace and lets developers inspect the code behind a Slack message, as well as browse Slack API docs within Slack.

What’s to come for developers with Salesforce and Slack

We have been busy investigating what it could mean to bring Slack and Salesforce together since Salesforce signed a definitive agreement to acquire Slack. However, since the transaction was not yet complete, our teams had to work independently. During this time, we have been planning services that make applications that integrate Slack and Salesforce easier to implement. We have also been laying the groundwork for the Slack-First Customer 360 experience that we have now launched. With the acquisition now complete, both Slack and Salesforce teams can work together to execute the plans, which means both Slack and Salesforce developers have plenty to look forward to in the coming weeks and months.

Conclusion

Combining the low code capabilities of Salesforce, the elastic run time of Heroku, and Slack’s user interface, developer toolsets, and API services, Salesforce developers are in a unique position to build apps that can provide a connected experience for employees and customers.

Finally, there is an opportunity for you to integrate your favorite developer tools, such as GitHub, continuous integration servers, IDE, and command-line tools (CLI) with Slack to boost your day-to-day productivity.

To get started with your Slack development journey, visit the below resources on Trailhead and Slack docs:

About the author

Paste the below in Slack Block Kit Builder to learn more about Mohith Shrivastava.

{
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "*Mohith Shrivastava* works as a Lead Developer Advocate at Salesforce. Mohith is a *Slack Certified Developer*. He is currently focusing on"
            }
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": " • Slack and Salesforce Integration \n • Heroku  \n • Developer Tooling"
            }
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": "You can follow him on Twitter *<https://twitter.com/msrivastav13 | @msrivastav13 >*"
            }
        }
    ]
}

Stay up to date with the latest news from the Salesforce Developers Blog

Subscribe