I have a Pdf attachment which is linked to an object in a Package. The public site is also part of the package so I do not have acces to query all files. I want to be able to let a public site guest user download the Pdf. I am trying to add the attachment to files but I am not able to query the ContentDocumentId what am I missing?

Aggregations:0|SELECT ContentDocumentId FROM ContentVersion WHERE Id = :tmpVar1 LIMIT 1 Error: System.ListException: List index out of bounds: 0


Attachment Att = [SELECT Id, Name, IsPartnerShared,IsPrivate, OwnerId, body FROM Attachment WHERE ParentId = :cxsAtt.id and name Like '%.pdf' Limit 1];
ContentVersion cVersion = new ContentVersion();
cVersion.ContentLocation = 'S'; 
cVersion.PathOnClient = Att.Name;
cVersion.Origin = 'H';
cVersion.OwnerId = Att.ownerId;//Owner of the file
cVersion.Title = Att.Name;
cVersion.VersionData = Att.Body;
Insert cVersion;

List<ContentVersion> conDocument = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:cVersion.Id Limit 1];

ContentDocumentLink cDocLink = new ContentDocumentLink();
cDocLink.ContentDocumentId = conDocument[0].id;
cDocLink.LinkedEntityId = att.ParentId;//Add attachment parentId
cDocLink.ShareType = 'I';
cDocLink.Visibility = 'SharedUsers';
Insert cDocLink;
  • At first, you seem to be querying conDocument[0].ContentDocumentId but you are using conDocument[0].id . May be thats the issue? Else, can you provide the error details also? Apr 24, 2021 at 10:42
  • @HermantJain Sorry that is a typo. I get a list exeption before the cDocLink. Error: System.ListException: List index out of bounds: 0
    – Thomas
    Apr 24, 2021 at 10:47

2 Answers 2


It may be due to view all data permission, to query a content version you need to add that permission, see the details below

By default, users (including users with the “View All Data” permission) can only query files they have access to, including:

Salesforce Files in their personal library and in libraries they're a member of, regardless of library permissions (API version 17.0 and later).

Salesforce Files they own, shared directly with them, posted on their profile, or posted on groups they can see (API version 21.0 and later).

Enable the Query All Files permission to let your View All Data users bypass the restrictions on querying files.

Query All Files returns all files, including files in non-member libraries and files in unlisted groups.

Users can’t edit, upload new versions, or delete files they don’t have access to.

View All Data permission is required to enable Query All Files.

Documentation from salesforce

  • I assigned myself the view all data permission set and did the same query but still do not see a ContentDocumentId so this is not the answer to my question
    – Thomas
    Apr 24, 2021 at 13:16

For anyone who bumps into this issue. I resolved this trough Contendistribution:

ContentVersion cVersion = new ContentVersion();
cVersion.Title = Att.Name; 
cVersion.PathOnClient = Att.Name;
cVersion.VersionData = Att.Body;
Insert cVersion;

ContentDistribution cd = new ContentDistribution();
cd.name = cVersion.Title;
cd.ContentVersionId = cVersion.id;
cd.PreferencesAllowOriginalDownload = true;
cd.PreferencesAllowPDFDownload = true;
cd.PreferencesAllowViewInBrowser = true;
insert cd;  
List<ContentDistribution>   cdList = [select id,DistributionPublicUrl from ContentDistribution where id =: cd.id];  
system.debug('cd.DistributionPublicUrl: ' + cdList[0].DistributionPublicUrl);
return cdList[0].DistributionPublicUrl;

You must log in to answer this question.

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