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:
@AuraEnabled
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.
EDIT
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:
Although I'm using the ID coming directly from ContentDocument
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.