Activity › Forums › Salesforce® Discussions › How to create a Chatter post from Apex in Salesforce?
Tagged: Chatter Content Post, Chatter Link Post, Chatter Post, Chatter Text Post, FeedItem, Object ID, ParentId, Salesforce Apex, Salesforce Custom Object
-
How to create a Chatter post from Apex in Salesforce?
Posted by Avnish Yadav on August 8, 2018 at 6:46 AMHow to create a Chatter post from Apex in Salesforce?
Karthik J replied 4 years, 9 months ago 6 Members · 5 Replies -
5 Replies
-
Hello Avnish,
The following code may help you:
//Adding a Text post
FeedItem post = new FeedItem();
post.ParentId = oId; //eg. Opportunity id, custom object id..
post.Body = ‘Enter post text here’;
insert post;//Adding a Link post
FeedItem post = new FeedItem();
post.ParentId = oId; // Record Id eg. Opportunity id, custom object id..
post.Body = ‘Enter post text here’;
post.LinkUrl = ‘http://www.infallibletechie.com’;
insert post;//Adding a Content post
FeedItem post = new FeedItem();
post.ParentId = oId; // Record Id eg. Opportunity id, custom object id..
post.Body = ‘Enter post text here’;
post.ContentData = base64EncodedFileData;
post.ContentFileName = ‘infallible.pdf’;
insert post;Thanks.
- [adinserter block='9']
-
Hello Avnish,
You can refer the following example to add Salesforce Chatter posts with links, urls and mentions:
public with sharing class ChatterUtils {
// makes a simple chatter text post to the specified user from the running user
public static void simpleTextPost(Id userId, String postText) {
ConnectApi.FeedType feedType = ConnectApi.FeedType.UserProfile;
ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();// add the text segment
ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = postText;
messageInput.messageSegments.add(textSegment);
ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
feedItemInput.body = messageInput;// post it
ConnectApi.ChatterFeeds.postFeedItem(null, feedType, userId, feedItemInput, null)}
// makes a chatter post with some text and a link
public static void simpleLinkPost(Id userId, String postText, String url, String urlName) {
ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
feedItemInput.body = new ConnectApi.MessageBodyInput();// add the text segment
ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
feedItemInput.body.messageSegments = new List<ConnectApi.MessageSegmentInput>();
textSegment.text = postText;
feedItemInput.body.messageSegments.add(textSegment);// add the attachment
ConnectApi.LinkAttachmentInput linkIn = new ConnectApi.LinkAttachmentInput();
linkIn.urlName = urlName;
linkIn.url = url;
feedItemInput.attachment = linkIn;// post it!
ConnectApi.ChatterFeeds.postFeedItem(null, ConnectApi.FeedType.News, userId, feedItemInput, null);}
// makes a simple chatter text post to the specified user from the running user
public static void mentionTextPost(Id userId, Id userToMentionId, String postText) {
ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();// add some text before the mention
ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = ‘Hey ‘;
messageInput.messageSegments.add(textSegment);// add the mention
ConnectApi.MentionSegmentInput mentionSegment = new ConnectApi.MentionSegmentInput();
mentionSegment.id = userToMentionId;
messageInput.messageSegments.add(mentionSegment);// add the text that was passed
textSegment = new ConnectApi.TextSegmentInput();
textSegment.text = postText;
messageInput.messageSegments.add(textSegment);ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
input.body = messageInput;// post it
ConnectApi.ChatterFeeds.postFeedItem(null, ConnectApi.FeedType.UserProfile, userId, input, null);}
// pass the user’s id or ‘me’ to get current running user’s news
public static ConnectApi.FeedItemPage getNewsFeed(String userId) {
return ConnectApi.ChatterFeeds.getFeedItemsFromFeed(null, ConnectApi.FeedType.News, userId);
}}
-
Hi,
I think you need this –
FeedItem post = new FeedItem();
post.ParentId = oid; //eg. Opportunity id, custom object id..
post.Body = ‘Enter post text here’;
insert post;Hope this helps.
-
Is that possible with batch.. I need to send chatter notifications when opp close it today.
I am very new to saleforce can you rpl on this postt
-
Hi this code sends post to everyone, How to send post to particular person in chatter? Please tell me I need this.
Log In to reply.