When Salesforce meets Slack - 3

If you haven't gone through the first two blogs of mine related to the Salesforce-Slack Integration using the OOB Configuration options, I'd strongly recommend that you do before heading to this blog as I'll be discussing about the Salesforce to Slack Customization options using Apex here.

There are multiple ways to do that as well. In this article, I'll only be discussing about the Salesforce to Slack integration using Webhooks. What this means is that, based on my logic in Salesforce, when a record meets the criteria, notifications will be sent to Slack. The other way round is possible as well. But, that's something for the upcoming blog.

Sooooo, let's get started!!!

My use case is this : The Sales team of XYZ company collaborate over Slack. There's a Web to Lead process in place through which the Leads come in to Salesforce. There's a duplicate rule in place to mark the Leads as duplicate based on a custom logic that's already in place. So, the Sales team want to be notified on Slack only when a Lead with status "New" comes in. Of course, this could be done through configuration as well, but in this use case, I'll explain the Webhook approach.

1. Install the app "Incoming Webhook" in your Slack app.

So, with this, we have now successfully obtained the URL to which we'll be making the callout to Salesforce.

**2. Now, let's build our apex class to perform this callout. **

public with sharing class SlackLeadPublisher {
    private static final String slackURL = WEBHOOK_URL;
    public class leads {
        @InvocableVariable(label='Lead Name')
        public String leadName;
        @InvocableVariable(label='Status')
        public String status;
        @InvocableVariable(label='Email')
        public String email;
    }
     
    @InvocableMethod(label='Post Lead to Slack')
    public static void postToSlack(List<leads> leadList) {
        leads l = leadList[0]; 
        Map<String,Object> msg = new Map<String,Object>();
        msg.put('text', 'The following lead has been created:
' + l.leadName + '
Status: *' + l.status + '*' + '
Email: *' + l.email);
        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();
            req.setEndpoint(url);
            req.setMethod(method);
            req.setBody(body);
            Http http = new Http();
            HttpResponse res = http.send(req);
        }
    }
}

PS : Don't forget to include the webhook URL in your remote Site Setting to allow callout.

**3. Process Builder - **The next step is to build a process builder that would invoke the Apex Class whenever a Lead is created with the Status "New"

Capture.PNG

**4. Create the Lead Record - **Now, go ahead and create a Lead record using your Web to Lead form and Voila, you should be able to see the notification on your Slack channel as follows:

Capture.PNG

I hope you found this blog useful. If you did, please leave your comments below. And if you have an interesting use case related to the integration, feel free to share.

Happy coding!

Receive my updates

Email me on neha.kumari@hey.com, or follow me via RSS, Twitter or Instagram.