Activity Forums Salesforce® Discussions How to create a Chatter post from Apex in Salesforce?

  • Prachi

    Member
    August 8, 2018 at 7:20 am

    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.

  • shradha jain

    Member
    August 8, 2018 at 7:27 am

    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);
    }

    }

  • shariq

    Member
    September 16, 2018 at 8:48 pm

    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.

  • Naveen

    Member
    May 25, 2021 at 4:35 pm

    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

  • Karthik

    Member
    August 12, 2021 at 4:30 am

    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.

Popular Salesforce Blogs