Slack and Salesforce Integration

Slack is a team collaboration application that has gained a significant amount of momentum over the last 2 years. One of the things that sets Slack apart is the simple and powerful way it can integrate with external applications (Google Drive, Twitter, Trello, or any other app). Using Incoming Webhooks, external apps can automatically post messages to Slack channels when specific events under their control occur. Using Slash Commands, users can query or update information in external apps from within the Slack UI. You can even set up Bots to listen to Slack conversations and automatically take actions in response to specific patterns. These integrations allow you to turn Slack into a centralized real-time messaging hub.

In this article, I demonstrate a simple integration scenario between Slack and Salesforce. As described above, the Slack and Salesforce integration can take different forms:

  • Salesforce to Slack. For example, automatically post messages to a Slack channel when specific events happen inside Salesforce (Account created, Opportunity status change, etc.)
  • Slack to Salesforce. For example, use a Slash Command to quickly access or update Salesforce data from within the Slack UI.

In this post, I’ll focus on the “Salesforce to Slack” scenario, leaving “Slack to Salesforce” for another post (now available here). As an example, we will create a custom integration that automatically posts a message to the #general Slack channel when the status of an opportunity changes in Salesforce.

Step 1: Create a Webhook

A Webhook is an endpoint that an external application (in this case, Salesforce) can use to POST messages to a Slack channel. The POST’s JSON payload includes the message text and some other options.

Here is my Webhook configuration for the Salesforce Opportunities integration:

All you have to do is specify the Slack Channel to post to: I chose the #general channel for this example. The Webhook URL is automatically generated. You can optionally provide a custom username for the integration (I named it “Salesforce Opportunities”) and upload a custom logo.

Step 2: Create the SlackOpportunityPublisher Apex Class

The second step is to create a simple Apex class that is capable of posting a message to the Webhook URL. The message includes the Opportunity details (Opportunity name and the new stage).

In the Developer Console, create a new Apex class called SlackOpportunityPublisher defined as follows:

public with sharing class SlackOpportunityPublisher {
    private static final String slackURL = 'YOUR_WEBHOOK_URL';
    public class Oppty {
        @InvocableVariable(label='Opportunity Name')
        public String opptyName;
        public String stage;
    @InvocableMethod(label='Post to Slack')
    public static void postToSlack(List<Oppty> oppties) {
        Oppty o = oppties[0]; // If bulk, only post first to avoid overloading Slack channel
		Map<String,Object> msg = new Map<String,Object>();
		msg.put('text', 'The following opportunity has changed:\n' + o.opptyName + '\nNew Stage: *' + o.stage + '*');
		msg.put('mrkdwn', true);
        String body = JSON.serialize(msg);    
        System.enqueueJob(new QueueableSlackCall(slackURL, 'POST', body));
    public class QueueableSlackCall implements System.Queueable, Database.AllowsCallouts {
        private final String url;
        private final String method;
        private final String body;
        public QueueableSlackCall(String url, String method, String body) {
            this.url = url;
            this.method = method;
            this.body = body;
        public void execute(System.QueueableContext ctx) {
            HttpRequest req = new HttpRequest();
            Http http = new Http();
            HttpResponse res = http.send(req);

Change “YOUR_WEBHOOK_URL” with the URL of the Webhook you created in step1.

This class knows how to post a message to Slack, but doesn’t know when to do it. We’ll take care of that in Step 3.

Step 3: Create a Simple Process in ProcessBuilder

The last step is to define a simple process that calls SlackOpportunityPublisher when the Stage of an opportunity is changed. Watch the video below to see how to easily create the process.

14 Responses to Slack and Salesforce Integration

  1. Vida March 8, 2016 at 2:39 pm #

    HI. Thanks for this tutorial. It was very helpful. I just wanted to know if using the process builder for posting webhooks has any advantages over using a trigger for this purpose.

  2. agario March 8, 2016 at 10:07 pm #

    good of nice blogees

  3. Romain March 24, 2016 at 12:53 pm #

    Hi Christophe and many thanks for that tricks….it works very well on my sandbox but as I’m not a developer at all, I don’t know how to write a test class for it…When I want to deploy it on production it says that my code coverage is 0%….Can you help me on that?

  4. Bryony Mackey April 8, 2016 at 11:55 am #

    This tutorial was super helpful, thank you!

    I had to add as a Remote Site under Setup–>Security–>Remote Site Settings

    Just in case that helps someone else out.

    • Romain April 12, 2016 at 3:57 am #

      Hi Bryony
      Could you help me with the test class to write in order to put it on production? It works well on my sandbox but i’m not able to deploy it on production because my code is not covered. As I am not a developer it s quite hard for me to handle that.

      Thanks in advance


      • Natalie May 2, 2016 at 9:29 am #

        Need the same… :(

  5. Bryony Mackey April 21, 2016 at 2:18 pm #

    Hi Romain —

    Apologies, I am just learning Apex myself and don’t know offhand how to write the test class, but if I get it done I’ll try to remember to post here.

  6. Natalie May 2, 2016 at 9:17 am #

    Hey, thanks! really helped!
    Now I need to deploy it to production and need to write tests for it. Do you have any test code that covers 75% of it?
    Thanks a lot

    • Ryan May 3, 2016 at 10:12 am #

      i second the need for some good code coverage.

  7. propchill May 19, 2016 at 7:29 am #

    Residential property in Pune in 2016, much the same as the earlier years, is achieving new statures. Numerous corporates and commercial ventures are moving to the city yet predominantly the IT division is the main thrust behind the speeding up of costs of property available to be purchased in Pune. In spite of the fact that the Indian economy is gazing upward, the scale at which the IT business is developing in the city is mind boggling and impossible. Call us- 0124-4370612

    Real Estate Developers In Pune


  1. Slack and Salesforce Integration – Part 2 | Christophe Coenraets - January 12, 2016

    […] my previous post, I demonstrated how to access Slack from Salesforce. I shared an example showing how to use Slack […]

  2. Writing a Salesforce Bot for Slack | Christophe Coenraets - April 19, 2016

    […] part 1, I demonstrated how, using a Webhook, Salesforce can post messages to Slack channels when specific […]

  3. Slack and Salesforce Integration - Developer Relations - May 5, 2016

    […] out this blog post to learn more about integrating Slack and Salesforce using […]

  4. Slack and Salesforce Integration Webinar | Christophe Coenraets - May 12, 2016

    […] and Salesforce Integration Part 1: Webhooks – Blog – Apex […]

Leave a Reply