3

Hi i am trying to implement Slack to Saleforce integration using below link. Everything works fine but i want to post a snippet instead of a text message, so that team can comment on it.

msg.put('mode', 'snippet');

In my R&N i found mode attribute but it is't working.

http://coenraets.org/blog/2016/01/slack-salesforce-integration/

@InvocableMethod(label='Post to Slack')
    public static void postToSlack(List<Lead> leads) {
        Lead l = leads[0]; // If bulk, only post first to avoid overloading Slack channel
        Map<String,Object> msg = new Map<String,Object>();

        msg.put('text',  'The following lead has been created update:\n' + l.leadName + '\nCompany: *' + l.leadCompany + '*');
        msg.put('mrkdwn', true);
        msg.put('mode', 'snippet');
        String body = JSON.serialize(msg);
        System.enqueueJob(new QueueableSlackCall(slackURL, 'POST', body));
    }

https://api.slack.com/types this link take you to supported types. Has anyone implemented slack from salesforce integration. Please help

3

After reading through the slack API docs, it doesn't appear that incoming webhooks support snippets. The mode argument doesn't exist for a message as far as I can tell.

A snippet is a type of File object in Slack, and you'd need to use the file.upload endpoint. This question on Stack Overflow says that it's impossible to upload a file via an incoming webhook (though it doesn't have any authoritative source on that). Using that endpoint means you need to use Slack's web API.

From the look of it, using Slack's web API ends up being very similar to using an incoming webhook. They're both REST APIs. The difference is that you need to jump through the additional hoops of authenticating with Slack (and managing the OAuth tokens), manually set the endpoints, and you need to manually specify the channel to post to in your request.

The process of setting up an app through Slack (to get access to the web api) would be off-topic here. Their documentation on this goes over the important details.

I think the only other thing worthy of note is that you'll need to add the base url for the Slack web api (https://slack.com/api/) to your org's remote sites in Salesforce.

The code that you've copy/pasted from Christophe Coenraets looks like a solid enough base (though it doesn't have anything to gracefully handle errors). Once you're set up, changing the url you send to QueueableSlackCall() is pretty much all that needs to be done (assuming you're building an appropriate request body).

The request body, being built from a Map<String, String>, could look something like this

msg.put('token', <OAuth token here>);
msg.put('filetype', 'text');
msg.put('filename', 'new SF lead for ' + l.leadCompany + ' - ' + l.leadName);
msg.put('title', 'new lead created');
msg.put('content',  'The following lead has been created update:\n' + l.leadName + '\nCompany: *' + l.leadCompany + '*');
msg.put('channel', '#somechannel');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.