0

I am wondering is that possible to create a chatter feed item that is the combination of Link post as well as Content post. Moreover, if that is possible what could be the ContentData. I am assuming contentdata is the Body attribute of the attachment object. is that right? Please suggest.

//Combining Content post and Link post
FeedItem post = new FeedItem();
post.ParentId = oId; //eg. Opportunity id, custom object id..
post.Body = 'Enter post text here';
post.LinkUrl = 'http://www.someurl.com';
post.ContentData = base64EncodedFileData;
post.ContentFileName = 'sample.pdf';

insert post;

1 Answer 1

2

You can with the ConnectAPI...that lets you create multiple message segments and upload an attachment in one post.

The code below creates a simple blob as an example, but you can use any content. I would also check out this git repo for easier segment creation

String s = 'Test my Attachment';
Blob b = blob.valueof(s);

//ConnectApi.BinaryInput(blob, contentType, filename)
    ConnectApi.BinaryInput bi = new ConnectApi.BinaryInput(b, 'txt', 'testfile1.txt');

    ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
    messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();

    ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
    textSegment.text = 'Enter post text here' + '\n';
    messageInput.messageSegments.add(textSegment);

    ConnectApi.LinkSegmentInput linkSegment = new ConnectApi.LinkSegmentInput();
    linkSegment.url = 'http://www.someurl.com';
    messageInput.messageSegments.add(linkSegment);

    textSegment = new ConnectApi.TextSegmentInput();
    textSegment.text = '\n' + 'Enter some more post text here';
    messageInput.messageSegments.add(textSegment);    

    ConnectApi.NewFileAttachmentInput cai = new ConnectApi.NewFileAttachmentInput();
    cai.title = 'My Chatter Text File';

        //Create new Feed Item Input and then post to Feed in Target Object  
        ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
        input.body = messageInput;
        input.attachment = cai;
        input.visibility = ConnectApi.FeedItemVisibilityType.InternalUsers; 

//The third param is the Record Id of the object you want to post to
//The fifth is the binary input that adds the attachment
    ConnectApi.FeedItem fi = ConnectApi.ChatterFeeds.postFeedItem(null, ConnectApi.FeedType.Record, 'a00w000000V32nN', input, bi);
6
  • Thanks a lot BritishBoyinDC. I understood better now. I have question though. If i want to create multiple feed items in a for loop for example, then how could this statement bulkified ? " ConnectApi.FeedItem fi = ConnectApi.ChatterFeeds.postFeedItem(null, ConnectApi.FeedType.Record, 'a00w000000V32nN', input, bi);"
    – Bforce
    Commented Aug 15, 2014 at 20:25
  • 1
    Based on the docs, there is no bulkification of Chatter - each post = 1 DML statement - so as long as you can keep within that, it should work. But as per docs, the ConnectAPI is also subject to rate limiting - ConnectApi rate limits match Chatter REST API rate limits. When you exceed the rate limit, a ConnectApi.RateLimitException is thrown so your Apex code must catch and handle this exception. Commented Aug 15, 2014 at 20:32
  • Just a note that in the upcoming Winter '15 release (API v32.0), bulk posting will be supported in Apex (ConnectApi) and also in the Chatter REST API.
    – alouie
    Commented Aug 16, 2014 at 6:50
  • Good to know - I am presenting on the ConnectAPI at DF14 - can I include that if I can emphasise the Safe Harbour? Commented Aug 16, 2014 at 19:40
  • @BritishBoyinDC Can we create two attachments like a PDF and a canvas in a single feed? Please advice. Thanks.
    – Bforce
    Commented Aug 18, 2014 at 18:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .