I have this flow in my customer community that loops over the quotes of a given opportunity and shows relevant information about every quote in a separate slide. This may include attachments uploaded through the Quote's related list "Attachments" and I created a lightning component that shows a download button linked to it.

Because the Attachment table does not support Quotes as parentId and because after some research I didn't find any other way to get to the document I found myself having to do this on my controller in order to get a download link:

public static String getDocumentURL(String parentId){
    List<ContentDocumentLink > contentDocumentLink = [select ContentDocumentId from ContentDocumentLink WHERE LinkedEntityId =: parentId LIMIT 1];
    List<ContentDocument> contentDocument = [select Id from ContentDocument WHERE Id =: contentDocumentLink[0].ContentDocumentId LIMIT 1];
    List<ContentVersion> contentVersion = [select Id from ContentVersion WHERE ContentDocumentId =: contentDocument[0].Id AND isLatest = true LIMIT 1];
    List<ContentDistribution> contentDistribution = [select Id, Name, ContentDownloadUrl from ContentDistribution WHERE ContentVersionId =: contentVersion[0].Id LIMIT 1];

    if (contentDistribution.isEmpty()) {
        // create new contentDistribution if none present
        ContentDistribution newContentDistribution = new ContentDistribution(Name = parentId, ContentVersionId = contentVersion[0].Id, PreferencesAllowViewInBrowser = true );
        insert newContentDistribution;

        ContentDistribution output = [SELECT Id, Name, ContentDownloadUrl, ContentDocumentId FROM ContentDistribution WHERE Id =: newContentDistribution.Id LIMIT 1];
        return output.ContentDownloadUrl;
    } else {
        return contentDistribution[0].ContentDownloadUrl;

I then use the ContentDownloadUrl in my lightning:button and this is actually working, despite the fact that the user receives an Email "Your Content Delivery Has Been Viewed" every time this link is called.

I'm pretty sure there has to be a better/more elegant solution to accomplish this and I would also prefer to preview the document instead of downloading it, if that is a possibility using Aura components.

Any guidance is much appreciated - Thanks in advance.


Another approach would be to get the ContentDocument's Id and then use that with lightning:openFiles, lightning:fileCard, etc. The issue here is that the ID I get from those Documents does not seem to get recognized by the components:

enter image description here

Although I'm using the ID coming directly from ContentDocument

enter image description here

I do not know if this has something to do with the quotes object itself, i will try with another object and update the post.

1 Answer 1


Since you already are playing with FileId [ContentDocumentId] (069XXXXXXXXXXXX), and want to preview the Attachment, lightning:fileCard might be what you are looking for, It has preview and Download options.


  • Thanks for the input. I actually tried something similar with lightning:openFile, but the problem is that the ID I get from ContentDocument does not work with those components. I'll edit the question with some Screen Shots. Commented Nov 9, 2020 at 15:24
  • 1
    I'm able to make it work with the ContentDocumentId and lightning:fileCard, Are we running the Controller in WithoutSharing Context ? Commented Nov 10, 2020 at 12:56
  • Thanks again for your answer. Yes, we are. Actually, the flow is executed as "System-Context without sharing - access all data" (also tried system context with sharing) and the controller is also running as "without sharing". I forgot to mention that this flow is run in a user community - I updated the question. Removing the "without sharing" from the controller does not seem to solve the problem and actually I have to run the flow in system context because afaik community users do not have read access to quotes/opportunities... Commented Nov 11, 2020 at 9:35

You must log in to answer this question.

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