Adaptal

How to Expose a File via Salesforce Flow

Best Practice

How to Expose a File via Salesforce Flow

Overview

Learn How to Expose a File via Salesforce Flow. As a Salesforce Consulting Partner, we encounter scenarios where clients need to upload files to Salesforce, collaborate on them, manage their versions, and keep track of important files, particularly for community members. Although there are tools out there to install, Salesforce provides standard functionality to handle these requirements through a few key objects:

ContentDocument is the primary object representing the file itself. ContentVersion tracks the different versions of the same file, allowing you to manage these versions effectively. ContentDocumentLink acts as a junction object linking a ContentDocument (file) to a record. When a file is uploaded directly to a record or shared with a record, Salesforce creates a ContentDocumentLink record, establishing a connection between the file and the record.

To share files with other records, you simply need to create a ContentDocumentLink between the file and the target record. Files uploaded to Salesforce can be viewed internally within the system. However, if you need to create public URLs for sharing files outside of Salesforce, additional steps are required to make the files accessible externally.

Making the Files Public:

To upload a file, navigate to the Files related list and use the upload function. This standard method allows you to attach the file directly to a record. Alternatively, you can utilize custom solutions, such as flows or Lightning Components, to manage file uploads according to specific needs or workflows.

How to Expose a File via Salesforce Flow

After uploading a file, you can create an unrestricted public link by selecting the “Public Link” option at the top of the file’s detail page.

How to Expose a File via Salesforce Flow

This action generates a public link for the specific file. With this link, anyone can view and download the file from the internet. You can copy the link and share it with anyone you wish.

How to Expose a File via Salesforce Flow

Everything was functioning as expected up to this point. However, there are a few issues to consider. Firstly, the process involves multiple clicks and manual copying of the link, which is not ideal and lacks automation. Secondly, if you create a formula field to display an image using this public link, the image will not render correctly. While this may seem like a limitation, there are alternative methods to address this issue internally.

How to Expose a File via Salesforce Flow

Using Flow to Generate Public URLs:

ContentDelivery (API name: ContentDistribution) is the object used to make files public. By creating a ContentDistribution record related to a ContentVersion, a public URL is generated for that specific file version. When using the Public Link button, it also creates a Content Delivery record.

To automate the process of uploading a file and generating a public URL, follow these steps to create a screen flow:

Screen Flow to Upload a File and Generate a Public URL

1. Create an input variable named `recordId`.
2. Add a screen element with a file upload component. Configure it to allow only a single file to ensure the flow operates with one file at a time.

How to Expose a File via Salesforce Flow

2. The standard file upload component does not have a setting to make it mandatory. To address this, you can include a decision element to verify whether the user has uploaded a file. Note that this step is optional and can be omitted if you prefer.

3. Even though you only allow a single file upload, Salesforce stores the IDs of content versions in a text collection named `ContentVersionIds`. To retrieve the specific content version record, use a Get Records element. Utilize the “In” operator, introduced in Winter ’23, to efficiently query the content version record.

How to Expose a File via Salesforce Flow

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. 

How to Expose a File via Salesforce Flow

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

How to Expose a File via Salesforce Flow

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, as a customer or Salesforce Partner, need. 

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

How to Expose a File via Salesforce Flow

7. Optionally, add a success screen. 

Result

Hope this helped you learn how to expose a file via Salesforce 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, we uploaded a house inspection form, flow created a public link, and the Image Preview formula field displays it. Feel free to contact us for further help.