I have been using this code to add the ability to take pictures of inventory once the customer has audited it-
However, we need to be able to post it as a FeedItem with an attachment instead of as an attachment to the record. The code above has been modified for my use, but most of the code is the same.
What I have tried at the end of the process is pretty much adding this-
//Original code leads up to update a but returns a.id... Attachment a = [ SELECT Id, Body, parentId, name FROM Attachment WHERE Id =: fileId ]; String existingBody = EncodingUtil.base64Encode(a.Body); a.Body = EncodingUtil.base64Decode(existingBody + base64Data); update a; version.Title=a.name; version.VersionData = a.Body; version.ContentUrl = url.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+a.id; upsert version; FeedItem post = new FeedITem(); post.body = 'File Upload'; post.ParentID = UserInfo.getUserID(); insert post; FeedAttachment postAttachment = new FeedAttachment(); postAttachment.FeedEntityId = post.id; postAttachment.Type = 'Content'; postAttachment.RecordId = version.id; insert postAttachment; return version.id;
I wasn't able to change the code to work for a ContentVersion instead of an attachment originally, so I moved to this. After I did this I did some digging and now I think that the ContentVersion needs either ContentUrl or PathOnClient. So, I added this-
version.ContentUrl = url.getSalesforceBaseUrl().toExternalForm()+'/servlet/servlet.FileDownload?file='+a.id;
Which is using the Url of the Attachment to create the ContentVersion, but I am still unable to get this to work. The error I am getting is internal server error 119852647. Any thoughts?