Slack is a cloud-based collaboration application meant to improve organisational communication and teamwork. It is a central hub for teams and individuals to interact, share files, and work on projects together. Slack has features such as channels for different topics or groups, direct messaging, file sharing, connectivity with other tools and services, and the ability to search and archive conversations to help speed collaboration. In this post, we will integrate Slack in Salesforce for Customized Push Notifications using Apex and Flow.
Steps required for Integrating Slack in Salesforce
We can integrate Slack in Salesforce using no code, low code as well as a pro code approach. In this post, we will use the low and pro-code approach in this post. Below steps are required for Slack integration
- Slack Channel Creation
- Create Slack Incoming Webhook
- Consume Webhook in Apex
- Consume Webhook in Flow
Let us complete all the above steps one by one.
1. Slack Channel Creation
We will push notifications to some channels where people are collaborating on some projects. If you have a Slack channel already for testing this POC then you can use it otherwise you can create a new Slack channel. If you are new to Slack then you can register it here for free.
Once you have access to Slack, open its application to create a channel. You can install the desktop version of the Slack application as well.
a) Click on Channels to create a new Channel similar to the below image.
b) Provide the meaningful name of the channel. We can put the project name, module name or client name etc.. for the channel name. If we want to add external people as well to our channel then select the checkbox “Invite external people.”
c) Select channel visibility type based on your requirement. If visibility is set as public then it will be visible to everyone. If it is private then it is not visible to anyone, it will only be visible who have an invitation link for the channel.
d) Add all required people to this channel. If all users will be part of this channel who are part of your slack workspace then enable the flag “Automatically add anyone who joins”. All users will automatically be added to the channel.
2. Create Slack Incoming Webhook
We need one Slack app to create an incoming webhook. This webhook will be used to post messages on Slack Channel.
Create a Slack App
A Slack app is a software application or program that extends and improves the capabilities of the Slack platform. These apps are designed to work in collaboration with Slack’s communication and collaboration features, allowing users to personalise their Slack experience, automate activities, and link Slack to other tools and services.
Create an app from https://api.slack.com/apps/ to post messages. Click highlighted Create New App button to create a new app.
Select From Scratch to create an app. This will allow us to put all detail manually.
Give a meaningful name and also select the workspace for this app.
Once App is created, we will create an incoming webhook. Select Incoming Webhook to create a webhook.
A webhook is an HTTP-based callback function that allows lightweight, event-driven communication between 2 application programming
Activate the incoming webhook by clicking on the below highlight activate flag (on radio button).
Add created webhook to the workspace.
Select the channel where Webhook will post messages in Slack.
Allow Slack app to post messages in selected channels.
This will create a webhook URL for posting messages in the Slack channel. We will use this URL in Apex and Flow to create notifications.
3. Consume Webhook in Apex
Let us create an apex class to integrate Slack into Salesforce. For this outbound Salesforce Integration, we need to perform the below steps
- Create a Named and External Credential
- Create a permission set for external callout
- Create a Slack Integration Apex class
- Test Apex class
Follow our blog Low Code Integration for Text Translation using SysTran API to complete the top 2 steps in detail.
Create a Named and External Credential
We will create an External Credential with the name External Callout EC. Check the below image for other details.
Create a named credential SlackChanneNC for Slack Webhook URL.
Create a permission set for external callout
Create a permission set Callout User PS and assign the above created external credential to External Credential Principal Access. Once an external credential is assigned, add this permission set to the user. For POC assign this permission set to yourself.
Create a Slack Integration Apex class
Create a service apex class for integrating Slack webhook.
Test Apex class to post Messages in the Slack channel
Let us use the above-created apex class to send messages into Slack Channel. This code block can be called from other apex classes to send messages into Slack Channel.
This will send a message in the Slack channel.
4. Consume Webhook in Flow
We can use the above-created apex class in Flow as well using the Invocable Apex method. Although we can use Webhook completely in Flow with no code approach. We will create another blog for this.
For this post, let us create an Invocable Apex method to send messages from the flow.
Consume Invocable Method in Salesforce Flow
We can use the above invocable method using the Action element in Flow. Place action element on flow designer and select action Post Message to Slack Channel.
Put dynamic messages based on your requirement. For this POC, I have assigned the hardcoded string “Hello from Flow”
When this flow will execute, it will run this action and post a message to the Slack channel.
Need some kind of help in implementing this feature, connect on my LinkedIn profile Dhanik Lal Sahni.