Wednesday, April 9, 2014

Post Chatter Feed with @Mention into a record in Salesforce

Posting a Chatter feed from Apex is a easy task. we can achieve this using the FeedItem Object. But is not so easy to add the chatter post along with @Mention  i.e to Some of the users.

One of the possible examples would be using a HTTP request call as described here.

Another best way is to use ConnectApi. With this the @Mention and posting the feed to a specific record is so easy.

a small part of my coding is,,,



            String userToMention = a.OwnerId;
            String subject = a.id;
         
            ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
            messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
         
            ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
            textSegment.text = 'Account Created by ';
            messageInput.messageSegments.add(textSegment);
         
            ConnectApi.MentionSegmentInput mentionSegment = new ConnectApi.MentionSegmentInput();
            mentionSegment.id = userToMention;
            messageInput.messageSegments.add(mentionSegment);
         
            ConnectApi.FeedItemInput inputdata = new ConnectApi.FeedItemInput();
            inputdata.body = messageInput;
         
            ConnectApi.FeedItem feedItemRep = ConnectApi.ChatterFeeds.postFeedItem(null, ConnectApi.FeedType.Record, subject, inputdata, null);


Post Chatter Feed with @Mention into a record in Salesforce




1 comment: