0

I have been using this code to add the ability to take pictures of inventory once the customer has audited it-

http://www.sfdcmonkey.com/2017/09/25/file-upload-lightning-component/

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?

0

I am using this method to create a File:

public static ContentDocumentLink createFile(Id parentId, String fileName, Blob body) {

    // insert a version of the file - if no ContentDocumentId, then it creates a new ContentDocument
    ContentVersion cv = new ContentVersion();
    cv.ContentLocation = 'S'; // S = within Salesforce, E = External
    cv.VersionData = body;
    cv.Title = fileName;
    cv.PathOnClient = filename;
    insert cv;

    cv = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id =: cv.Id LIMIT 1];
    ContentDocumentLink cdl = new ContentDocumentLink();
    cdl.ContentDocumentId = cv.ContentDocumentId;
    cdl.LinkedEntityId = parentId;
    cdl.ShareType = 'V';
    insert cdl;

    return cdl;
}
  • This works to create a file, but how can this be used to create a feeditem and attach the file to it? – Josh Schoenbachler Dec 7 '17 at 18:23
0

There is a new Lightning Base Component that is doing just that and is handling all the hard stuff for you. Have a look to lightning:fileUpload.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.