How To Create chatter feeds (FeedItem) Or SF File From Attachment?

Snippet Code for Creating FeedItem.


- Most of time user easily create feedItem but it seems that file created is not Previewable.

       The only solution to it is add extension to ContentFileName.




for(Attachment att : [SELECT id,body,parentId FROM ]
{
 Blob body = att.body;

            String name = (att.name).substringBeforeLast('.');
            String extension = (att.name).substringAfterLast('.');
            
            FeedItem file = new FeedItem();
            file.ParentId =  String.valueof(att.parentid) ;   // ID of parent object of file (Record Id)
            file.ContentData = body;
            file.ContentFileName = name + '.' + extension;
            file.Title = name;
            System.debug('file::' + file);
            
            feedItemList .add(file);
}

If(feedItemList.size() > 0)
{
insert feedItemList;
}


File will get Show up in File related list of Record(Parent id).

No comments:

Post a Comment