1

Based on some answers and google searching, I've understood that all content documents attached to a record can be downloaded if a url is with '/' separated content document IDs formed as below :

https://xxxxxxxxxxxx/sfc/servlet.shepherd/document/download/06AN0000002deibMAA/06AN00000030E6BMAU/06AN00000030E6pMAE/06AN00000030E6uMAE?

To create a button out of it, I've written a function :

public static List<Id> getContentDocumentLinks(Id loanId) {
        try {
            String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
            List<ContentDocumentLink> contentDocumentLinks = [  SELECT Id, COntentdocumentId 
                                                                FROM  ContentDocumentLink 
                                                                WHERE LinkedEntityId = :loanId];
            List<Id> contentDocIds = new List<Id>();
            for (ContentDocumentLink cdl : contentDocumentLinks) {
                contentDocIds.add(cdl.Id);
            }
            String downloadUrl = baseUrl+'/sfc/servlet.shepherd/document/download/'+String.join(contentDocIds,'/');
            System.debug('downloadUrl : '+downloadUrl);
            return contentDocIds;
        } catch (Exception ex) {
            System.debug('Error : '+ex.getMessage() + ' --> '+ ex.getStackTraceString());
            throw new downloadDocsException(ex.getMessage());
        }
   }

But opening that url while i'm logged in as the administrator gives me error - This content pack is empty or contains only links and Google Docs, and cannot be downloaded.

enter image description here

How could this be fixed ??

2
  • so, are any of the ContentDocumentIds associated to the loanId a ContentDocument of type Link or GoogleDoc? You may need to filter those out before adding to the URL string
    – cropredy
    Commented Sep 15, 2020 at 16:53
  • All the documents have been uploaded manually through an LWC that uses lightning-file-upload
    – N_H1922
    Commented Sep 16, 2020 at 4:37

1 Answer 1

2

I made a mistake in the code by adding the ContentDocumentLink's Id in the list instead of adding ContentDocumentLink.contentDocumentId.

Fixed code :

public static List<Id> getContentDocumentLinks(Id loanId) {
        try {
            String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
            List<ContentDocumentLink> contentDocumentLinks = [  SELECT Id, COntentdocumentId 
                                                                FROM  ContentDocumentLink 
                                                                WHERE LinkedEntityId = :loanId];
            List<Id> contentDocIds = new List<Id>();
            for (ContentDocumentLink cdl : contentDocumentLinks) {
                contentDocIds.add(cdl.COntentdocumentId);   // Add contentdocumentId
            }
            String downloadUrl = baseUrl+'/sfc/servlet.shepherd/document/download/'+String.join(contentDocIds,'/');
            System.debug('downloadUrl : '+downloadUrl);
            return contentDocIds;
        } catch (Exception ex) {
            System.debug('Error : '+ex.getMessage() + ' --> '+ ex.getStackTraceString());
            throw new downloadDocsException(ex.getMessage());
        }
   }
1
  • 1
    Thank you, thank you, thank you, thank you! Such a simple thing but this comment/answer helped me solve a problem I've been working on for days.
    – dBeltowski
    Commented Oct 3, 2022 at 19:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .