I'm using the following code to generate ContentVersion.

    String title = 'sample file.txt';
    ContentVersion contentVersion = new ContentVersion(
            VersionData = Blob.valueOf(jsonBodyPretty),
            Title = title,
            ContentLocation = 'S',
            PathOnClient = title

    insert contentVersion;

But when I try to preview it from Salesforce 'No Preview is Available'. If I download file and re-upload - everything is ok.

Have someone was able to create Previews from Apex for ContentVersion?

  • 1
    I have the same issue. Does anybody know a solution? – Mikhail Ivanov Oct 30 '18 at 14:47

you need the contentDocumentID for the preview, after insert the contentVersion get the ContentDocumentId

Id cdID = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id =: contentVersion.Id].ContentDocumentId;

I hope this help you.

  • And what do I need to do with Content Document in order to enable preview? – ytiq Aug 22 '18 at 10:22
  • You can use lightning:openFiles developer.salesforce.com/docs/component-library/bundle/… – Aida Aug 22 '18 at 14:06
  • I wrote that the problem with Preview text generation and not with Previewing file itself. I can preview it from standard view. – ytiq Aug 22 '18 at 14:13
  • I am having the same issue, when file was uploaded using apex no preview, but same file using file upload, give preview, how to find the issue? – Ankuli Dec 10 '20 at 16:47

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.