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 ??

  • 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 Sep 15 at 16:53
  • All the documents have been uploaded manually through an LWC that uses lightning-file-upload – N_H1922 Sep 16 at 4:37
1

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());
        }
   }
| 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.