Using Flow to Generate Public URLs for Files

Using Flow to Generate Public URLs for Files

Using Salesforce Files, you can upload files to Salesforce, share and collaborate on files, manage file versions, and follow important files. There are a few objects that you should be familiar with. ContentDocument is the main object that represents the file itself. The second object is ContentVersion. This object represents the versions of the same file and lets you manage the versions. The third object is called ContentDocumentLink. When you upload a file directly under a record or when you share a file with a record, Salesforce creates a ContentDocumentLink record in the system. ContentDocumentLink is a junction object between a ContentDocument (file) and a record. In order to share files with other records, you just have to create a ContentDocumentLink between the file and the target record. Read this post to learn how to share files with records using flow. When you upload files to Salesforce, they can be viewed internally. However, what if you want to create public URLs and share them outside of Salesforce?

Making the Files Public

In order to upload a file, you can go to the Files related list and upload the file. This is the standard way of uploading a file under a record. However, you can use other custom solutions as well. For instance, flow, Lightning Component, etc.

Upload Files from Record Page

After uploading a file, you can create an unrestricted public link by clicking the Public Link option on top.

Standard Way of Creating Public URL

It will generate a public link for that specific file. With this public link, anyone can view and download the file on the internet. You can copy the link and share it with anyone that you want.

Public Link Sharing

Everything was fine until this point. However, here are the problems. First of all, you had to click a few places, and then you copied the link manually. Too many clicks and nothing automated. Secondly, if you create a formula field that displays an image, you will see that it cannot display the image using this public link. However, this one shouldn't be a big issue since there are other ways to make it work internally.

Image Preview is Broken

Using Flow to Generate Public URLs

Content Delivery (API name is ContentDistribution) is the object that makes the files public. Once you create a ContentDistribution record related to the ContentVersion, it creates a public URL for that specific version of the file. When you create a public link using the Public Link button (the option that was shown before), it creates a Content Delivery too. Let's see how to automate this process.

Screen Flow to Upload a File and Generate a Public URL

Let's create a screen flow that lets the user upload a file. Then it should generate a public link for the file, and save the link to a URL field.

1- Create an input variable called recordId, add a screen element with a file upload screen component. Let's not allow multiple files, so that the flow will work with one file.

Screen Flow to Upload File

2- There is no setting to make the standard file upload component mandatory. Because of this, you can add a decision to check if the user uploaded a file. However, this step is optional.

3- Even if you don't allow uploading multiple files, Salesforce stores the Ids of the content versions in a text collection called ContentVersionIds. Therefore, you have to get that content version record using a Get Records element. You can use the In operator that Salesforce released in Winter '23.

Get the Content Version Record

4- Create a ContentDistribution record with the values from the ContentVersion record that you got in the previous step. Don't forget to make PreferencesAllowViewInBrowser true. You can set the other preferences as well.

Create Content Delivery

5- Get the Content Delivery that you created in the previous step.

Get the Content Delivery

6- Content Delivery has two fields that get auto populated. DistributionPublicUrl stores the public url of the file. Anyone with this link can view the file in a container. However, it is not possible to use this public URL in an img tag. The second auto populated field, ContentDownloadURL, contains the link to download the file and it is possible to use it in an img tag. Therefore, this is the field that we need.

Add an Update Records element to update your record. In this example, value of ContentDownloadURL is saved to Profile_Picture__c field.

Store the Public URL

7- Optionally, add a success screen.

At the end, your flow should look like this.

End of the Flow


This flow saves the file under the record, creates a public URL, and saves it to a custom field. You can create a formula field that displays the image from the link. As you can see, I uploaded my profile picture, flow created a public link, and the Image Preview formula field displays it.

Profile Picture Preview from Doctorium

Content Delivery object stores the public URLs of the files. However, it stores even more information about these public URLs. For example, you can see the first view date, last view date, and total view count. On the other hand, you can put a password for sensitive files or add an expiration date to the link. All these features make the Content Delivery a very useful object in Salesforce.

Be the first to comment

Leave a Reply

Your email address will not be published.