function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Naresh AVNaresh AV 

Insert a copy of Attachment into a Chatter Feed as a File for Sharing on custom object

Hi,

I need to add 'custom object' attachments to the chatter feed automatically. I have the following code but i'm facing some issues 

trigger AttachFileToCustomFeed on Attachment (after insert) {
 ID customId;
 list<FeedItem> listOfFeedFiles = new List<FeedItem>();
 if(Trigger.isAfter){
    for(Attachment attachment : trigger.new){
        string checkIfCase = string.valueof(attachment.Description); {
        customId= attachment.ParentId;
        FeedItem post = new FeedItem();
        post.ParentId = customId;
        post.Body = 'Attachment added';
        post.Type = 'ContentPost';
        post.ContentData = attachment.body;
        post.ContentFileName = attachment.Name;
        post.Title = attachment.Name; listOfFeedFiles.add(post);
     }
   } 
  }
  if(listOfFeedFiles!=null){
      insert listOfFeedFiles;
  }
}

but i'm gettiing the below error
'Invalid field ContentData for SObject FeedItem'
If I comment the 'ContentData' then i'm getting this
'Invalid field ContentFileName for SObject FeedItem'.
Can anyone please help in this issue.
Thanks in advance.
Best Answer chosen by Naresh AV
Naresh AVNaresh AV
Issue Resolved!!!

If anyone facing the same issue, please change the version for the trigger then it will work