Centro Support Center

Contact Us

Quick Start: Posting to Slack from Chatter Mentions

In Salesforce and Slack, it's common for users to want to relay Chatter Mentions and Posts to Slack so that nothing is missed. If you're looking for an easy solution, Centro has you covered with our app.

With Centro, you can seamlessly integrate Salesforce Chatter and Slack, ensuring that all important updates and conversations are shared between the two platforms. By setting up this integration, you can stay connected and up-to-date no matter which platform you prefer to use.

Wondering how to get started? We've got you covered. Check out this helpful video that demonstrates how to set up the integration and start relaying Chatter Mentions and Posts to Slack:

Additionally, to make the integration work smoothly, you'll need to use an Apex class in your Flow. This Apex class is responsible for detecting a mention user in Chatter and looking up the corresponding user in Salesforce. See below for the class and test class to use within your flow:

Apex Class: Chatter_GetMentions:

global inherited sharing class Chatter_GetMentions {
    
    @InvocableMethod(label='Chatter Get Mentions' description='Extract @ Mentions from Chatter Post')
    global static List<Response> execute(List<Request> requests) {

        List<Response> responses = new List<Response>();

        for(Request curRequest: requests ){
           
            Response curResponse = new Response();

            String feedItemId = curRequest.feedItemId;
            List<String> userIdList = new List<String>();
            
            ConnectApi.FeedElement feedItem = ConnectApi.ChatterFeeds.getFeedElement(Null, feedItemId);
            List<ConnectApi.MessageSegment> messageSegments = feedItem.body.messageSegments;
            
            for (ConnectApi.MessageSegment messageSegment : messageSegments) {
                
                if (messageSegment instanceof ConnectApi.MentionSegment) {
                    ConnectApi.MentionSegment mentionSegment = (ConnectApi.MentionSegment) messageSegment;
                    //System.debug('Mentioned UserId: ' + mentionSegment.record.id);
                    
                    userIdList.add((String)mentionSegment.record.id);
                }
            }
            System.debug(userIdList);
            curResponse.userIdList = userIdList;

            responses.add(curResponse);
        }
        return responses;
    }   

    global class InvocableActionException extends Exception{}

    global class Request {
        @InvocableVariable (label='Feed Item Id') global String feedItemId;
        }
    
    global class Response {
        @invocableVariable
        global List<String> userIdList;
    }
}

Apex Test Class: Chatter_GetMentionsTest:

@isTest
public with sharing class Chatter_GetMentionsTest {
    @isTest(SeeAllData=true)
    static void executeTest() {
        FeedItem f = new FeedItem();
        f.Body = 'legal test';
        f.parentID = UserInfo.getUserId();
        insert f;
        List<Chatter_GetMentions.Request> requests = new List<Chatter_GetMentions.Request>();
        Chatter_GetMentions.Request req = new Chatter_GetMentions.Request();
        req.feedItemId = f.Id;
        requests.add(req);
        Test.startTest();
        List<Chatter_GetMentions.Response> result = Chatter_GetMentions.execute(requests);
        Test.stopTest();
        System.assertEquals(true, result != null);
        System.assertEquals(1, result.size());
        System.assertEquals(true, result[0].userIdList != null);
        System.assertEquals(0, result[0].userIdList.size());
    }
}

As the video highlights, if you want to be alerted in Slack whenever a user is mentioned, you'll need to detect that event in Salesforce flow with a record triggered flow on the Feed Item object. The custom apex will parse the Chatter post to find @mentions, to return a User ID. On the User record, Centro ships a Slack Member Id that will be leveraged in the Slack notification. (Need help bulk mapping Salesforce User record to Slack User record? Quick Start: Bulk Mapping Slack User Ids to Salesforce Users!) 

If you have any further questions or need additional assistance with setting up this integration, please don't hesitate to get in touch. Our team is here to help and will be happy to guide you through the process.