We have a requirement where we need to attach multiple files and store them under Files (related list) for particular Sobject.

I am able to insert the file but i am not getting the preview option neither when i am downloading the file is visible. Please help me out..

Adding debug log for reference. CODE:

 Public String fileObj {get;set;}
    Public String contentLocation {get;set;}
    Public String fileName {get;set;}
     Public void UploadDocuments(){
            String filesUploadedList;
            String name = 'deepak';
            system.debug('\''+'/'+name+ '\'');
            String oppId;  
            fileObj  = apexpages.currentPage().getParameters().get('fileObject');
            contentLocation = apexpages.currentPage().getParameters().get('contentLocation');
            fileName = apexpages.currentPage().getParameters().get('fileName');
            system.debug('fileObj '+fileObj);
            system.debug('contentLocation '+contentLocation);
            system.debug('fileName '+fileName);
            //system.debug('fileObj '+JSON.stringify(fileObj));
            //oppId = apexpages.currentpage().getparameters().get('id');    
            oppId = '0069D000001rkq6QAA';
            ContentVersion Cv = new ContentVersion(ContentLocation = contentLocation,
                                                   VersionData = Blob.valueof(fileObj),PathOnClient = fileName,Title = fileName);

            insert Cv;

            ContentDocumentLink Cdl = new ContentDocumentLink();
            list<ContentVersion> contentVersionList = [SELECT id,ContentDocumentId FROM ContentVersion WHERE Id = :Cv.id];
            List<ContentDocumentLink> toBeInsertedContentDocumentList = new List<ContentDocumentLink>();
            system.debug('contentVersionList '+contentVersionList);
            for(integer i=0;i<contentVersionList.size();i++){
                Cdl.ContentDocumentId = contentVersionList[i].ContentDocumentId;
                Cdl.LinkedEntityId = oppId;
                Cdl.ShareType = 'V';
                Cdl.Visibility = 'AllUsers';

            insert toBeInsertedContentDocumentList;

            //return null;


enter image description here


Change your share type of ContentDocumentLink from "V" to "I". You can refer this document for additional help

Make this below change in your code

Cdl.ShareType = 'V';


Cdl.ShareType = 'I';
| improve this answer | |
  • I tried but that didnot help – Deepak Agarwal Jan 16 '19 at 14:11
  • Hi Deepak, Did you try uploading files with other file format, like PDF, if yes, then does it work ? Is it visible in the files related list ? And finally, does the user have the right permissions to the related record ? Here is the link mentioning the supported formats for content files help.salesforce.com/… – Vijay Ganji Jan 16 '19 at 14:41
  • The issue is resolved i had to change the conversion of file here VersionData = EncodingUtil.base64Decode(fileObj) instead of VersionData = Blob.valueof(fileObj) – Deepak Agarwal Jan 17 '19 at 8:51

Changed the code in class to below line:-

VersionData = EncodingUtil.base64Decode(fileObj)

Instead of

VersionData = Blob.valueof(fileObj)

Which helped me to preview and even download the file.

| improve this answer | |

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.