• Home
  • Blog
  • How to Integrate Slack with Salesforce: A Guide to Streamlining Your Business Processes
How to Integrate Slack with Salesforce: A Guide to Streamlining Your Business Processes

How to Integrate Slack with Salesforce

Collaboration tools have become essential in today’s workplace. Slack and Salesforce are two of the most popular tools used by businesses. Integrating Slack with Salesforce can improve communication, streamline workflows, and increase productivity.

Why Integrate Slack with Salesforce?

Integrating Slack with Salesforce offers many benefits, such as:

Improved Communication:

Sales reps can receive real-time updates on leads and opportunities in Slack. They can also communicate with their team members directly in Slack, without having to switch between different tools.

Streamlined Workflows:

Support teams can create cases and assign them to team members directly from Slack. This reduces the time it takes to create a case and ensures that the case is assigned to the right team member.

Increased Productivity:

A salesforce integration service consultant in USA suggests Integrating Slack with Salesforce as it eliminates the need for manual data entry, which can be time-consuming and prone to errors. This allows teams to focus on more important tasks. Requirement: My requirement is to create a new event for an account in Salesforce when a new message is generated in a Slack channel with the same name as the account record. To do this, I will need to integrate Slack with Salesforce. Solution: In my requirement, I will create an Apex class that is triggered at specific intervals, such as every hour or half hour. When the class runs, it should fetch all new messages in a Slack channel that arrived during that particular interval.
  1. The first step in the integration process is to create a new app in Slack. To do this, visit https://api.slack.com/apps and create a new app. Slack will provide you with a Client ID and Client Secret Key, which you will need in the further steps of the integration process.
app credentials In addition, you will need to set the appropriate scope for the app in the OAuth & Permissions section of the app settings. This will ensure that the app has the necessary permissions to access and modify the data in your Slack workspace and Salesforce org. Bot Token Scopes
  1. channels:history
  2. channels:read
  3. cconnect:read
  4. groups:history
  5. groups:read
  6. im:history
  7. im:read
  8. mpim:history
  9. mpim:read
User Token Scopes
  1. channels:history
  2. channels:read
  3. groups:history
  4. groups:read
  5. im:history
  6. im:read
  7. mpim:history
  8. mpim:read
This slack app also provides you with a User OAuth Token which will be used in further steps. user oauth token The first step is to obtain all channel IDs from Slack because we want the message to arrive in any Slack channel that is used in Salesforce. To do this, we need to use the SLACK_API_TOKEN variable, which is the Slack API token that you obtain from the Slack app. We can make HTTP requests to obtain all channel IDs from Slack. Http http = new Http(); HttpRequest req = new HttpRequest(); req.setMethod(‘GET’); req.setEndpoint(‘https://slack.com/api/conversations.list’); req.setHeader(‘Authorization’, ‘Bearer ‘ + SLACK_API_TOKEN); req.setHeader(‘Content-Type’, ‘application/json’); HTTPResponse res = http.send(req); Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(res.getBody()); List channels = (List) responseMap.get(‘channels’);We can use a list to store all the channel IDs from Slack.Next, we need to write Apex code to store this Slack channel data from an account object or any other custom object.After obtaining the channel IDs, we need to get the channel names from Slack. To do this, we need to make an HTTP callout to get the Slack channel names.Map<String, Object> infoParams = new Map<String, Object> {‘channel’ => channelId,’token’ => apiToken};Http http = new Http();HttpRequest infoRequest = new HttpRequest();infoRequest.setEndpoint(‘https://slack.com/api/conversations.info’);infoRequest.setMethod(‘GET’);infoRequest.setHeader(‘Authorization’, ‘Bearer ‘ + SLACK_API_TOKEN );infoRequest.setTimeout(60000);String infoBody = ”;for (String key : infoParams.keySet()) {if (infoBody.length() > 0) {infoBody += ‘&’;}infoBody += key + ‘=’ + String.valueOf(infoParams.get(key));}infoRequest.setBody(infoBody);HttpResponse infoResponse = http.send(infoRequest);string channelName;Map<String, Object> infoData = (Map<String, Object>) JSON.deserializeUntyped(infoResponse.getBody());Map<String, Object> channel = (Map<String, Object>) infoData.get(‘channel’);channelName = (String) channel.get(‘name’);As a prominent salesforce consultant in USA, we schedule our Apex class to run every half hour so that the below code can get any new messages that arrive in any Slack channel. You need to call the below code for each Slack channel you want to monitor.Datetime currentTime = Datetime.now();Datetime fiveMinutesAgo = currentTime.addMinutes(-30);String fiveMinutesAgoTimestamp = String.valueOf(fiveMinutesAgo.getTime()/1000);String channelMessages = ”;Map<String, Object> historyParams = new Map<String, Object> {‘channel’ => channelId, // Pass Your slack channel Id’limit’ => 1000,’inclusive’ => true,’token’ => apiToken,’oldest’ => fiveMinutesAgoTimestamp};Http http = new Http();HttpRequest historyRequest = new HttpRequest();historyRequest.setEndpoint(‘https://slack.com/api/conversations.history’);historyRequest.setMethod(‘GET’);historyRequest.setHeader(‘Authorization’, ‘Bearer ‘ + SLACK_API_TOKEN );historyRequest.setTimeout(60000);String historyBody = ”;for (String key : historyParams.keySet()) {if (historyBody.length() > 0) {historyBody += ‘&’;}historyBody += key + ‘=’ + String.valueOf(historyParams.get(key));}     historyRequest.setBody(historyBody);HttpResponse historyResponse = http.send(historyRequest);Map<String, Object> historyData = (Map<String, Object>) JSON.deserializeUntyped(historyResponse.getBody());List messages = (List) historyData.get(‘messages’); String channelMessages; for (Object message : messages) { Map<String, Object> messageData = (Map<String, Object>) message; channelMessages = channelMessages + ‘-‘ +(String) messageData.get(‘text’); } You now need to update the account object record with the same name as the Slack channel and utilise its field to create event data based on the Slack channel message