1

I apologize if this has already been covered (It probably has, I just cant find it in the 'Open your eyes' section of the forum)

But I'm having troubles creating a list of publicly accessible URLS generated from salesforce.

I know the urls's that we are after are stored in the ContentDistribution Object ('ContentDownloadUrl').

List<ContentDocumentLink> cdl = [
SELECT
    ContentDocument.Id, ContentDocument.Title, ContentDocument.FileType, ContentDocumentId
FROM ContentDocumentLink
WHERE LinkedEntityId =: recordId];

Here I can grab a list of the ContentDocuments.

Now what I Think I need from this is a New list of ContentDocumentId's which I can then query the content Distribution object

List<ContentDistribution> contentDist = [
SELECT
    Id, ContentDownloadUrl
FROM ContentDistribution
WHERE ContentDocumentId IN contentDocumentIdList];

What I'm struggling with is creating the list of ContentDocumentIds from the first query and then using that to get the list of Content Distribution (If this is even the correct way to get a list of publicly available URLS)

Its a great file storing solution with the ability to create new versions and share different versions of documents and images to different groups but I just find it so confusing.


Update:

Thanks to Adrian

I Was able to get a list of downloadable public url's for my files attached to a record.

  List<ContentDocumentLink> links = [
    SELECT ContentDocumentId FROM ContentDocumentLink
    WHERE  LinkedEntityId =recordId ];
        system.debug('CDL'+links);

 Set<Id> documentIds = new Set<Id>();

 for (ContentDocumentLink link : links)
    documentIds.add(link.ContentDocumentId);

 system.debug('documentIds'+documentIds);

List<ContentDistribution> distributions = [
    SELECT ContentDownloadUrl,DistributionPublicUrl
    FROM ContentDistribution
    WHERE ContentDocumentId IN :documentIds ];

system.debug('distributions'+distributions);

Set<String> urls = new Set<String>();

for (ContentDistribution urlLink : distributions)
    urls.add(urlLink.ContentDownloadUrl);

    system.debug('URLS' + urls);

1 Answer 1

2

One of the most common patterns you will use in Apex is to generate a Set<Id> from a List<SObject>, then pass it in to some query as a filter value.:

List<ContentDocumentLink> links = [
    SELECT ContentDocumentId FROM ContentDocumentLink
    WHERE ...
];
Set<Id> documentIds = new Set<Id>();
for (ContentDocumentLink link : links)
    documentIds.add(link.ContentDocumentId);
List<ContentDistribution> distributions = [
    SELECT ContentDownloadUrl
    FROM ContentDistribution
    WHERE Id IN :links
];

That said, SOQL acutally supports joining these two queries into one (on most models, anyway).

List<ContentDistribution> distributions = [
    SELECT ContentDownloadUrl
    FROM ContentDistribution
    WHERE Id IN (
        SELECT ContentDocumentId
        FROM ContentDocumentLink
        WHERE ...
    )
];

You must log in to answer this question.

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