Uploading and Working with Files in Flow: A Sampler of Design Patterns

In this mini series of posts, I’m covering some basic ways to use Flow to work with uploaded files.

Sure, you’ll learn how to upload a file, but also so much more:

  • Understand the reasons to use Flow to upload files
  • Learn some basics about the Salesforce Files data model
  • Install and try out 5 sample flows for uploading and manipulating files
  • Review a few good ideas that need your vote
  • Get some suggestions for how to go even bigger with file uploads

Once you install and setup this package (see below), you can jump to one of the posts in this series to dive right in:


Why Upload Files with Flow?

Mitch: you can upload a file right on a record’s File related list! Why bother? That related list upload feature is great, but it’s basic. Your Salesforce record doesn’t know or really care that a file was uploaded. And when a file is really necessary or required, how are you planning on enforcing that?

Upload files with Flow to…

  • Seamlessly integrate file upload steps into a larger Flow-based user process
  • Require certain files to be uploaded
  • Collect additional user-provided details about files using custom fields
  • Rename files to standardize how they are stored and appear in related lists
  • Automate how files are shared with all the right users and related records
  • Do cool stuff!

Some Basics About the Salesforce Files Data Model

The first step to success is understanding what objects are involved in working with Salesforce Files, which is where Salesforce stores a file you upload. There are several you may want to learn about, but these are the key objects:

When you upload a file from a record’s Files related list, it creates a ContentDocument; the file details are stored as a related ContentVersion; and its relationship to the current record is stored as a ContentDocumentLink.

ContentDocument – This is the main record of the file you’ve uploaded to Salesforce Files.

  • Think of it as a container or envelope for the file, all previous versions of it, and any metadata. Fields on the ContentDocument record are updated to reflect the latest ContentVersion record.
  • Key field(s): Id, LatestPublishedVersionId

ContentVersion – A version of the file you’ve uploaded as a ContentDocument.

  • Each file can have different versions, and all of those previous versions are available to your Salesforce users. This is great for a lot of reasons. Just think: not only do your users have access to the change history for your file, but you never have to worry about deleting an old file and creating a new one – your users can just keep coming back to the same file, the same URL, and see the latest version (and access the previous ones).
  • You can create custom fields on ContentVersion.
  • You can use record types on ContentVersion.
  • Key field(s): ContentDocumentId, Id, IsLatest

ContentDocumentLink – This is a junction object that links your file (ContentDocument) to records in Salesforce.

  • It is literally an association that shares it with the record and users that have access to that record.
  • The “Linked Entity” can be a record in a standard object, a custom object, a user… pretty much anything.
  • As a junction object, it facilitates one-to-many relationships: a ContentDocument only has to be uploaded and stored once, but it can appear wherever it is needed by your users.
  • Key field(s): ContentDocumentId, LinkedEntityId, ShareType, Visibility (so, ya know, all of them)

Sample Flows – How to Install/Access

Enjoy this Whitman’s Sampler of flows to get a taste for what’s possible (but be careful, one of the flows has a coconut filling). They might look similar at first glance, but I promise they are different and they build on each other.

INSTALL UNMANAGED PACKAGE
(THIS SOFTWARE PACKAGE IS COVERED BY THIS DISCLAIMER)

After installation, add the E01_000_SampleFlowLauncher flow to an active Case Lightning record page – this will present you with a menu of all sample flows to use.

When adding any of these flows to a Lightning page layout, in the component properties, you will need to check the “Pass record ID into this variable” checkbox for the caseRecordId input variable.

These sample flows are setup to associate files to Cases, but these same patterns will work with any object. Also, since these are fully functional, you could add them to an existing flow using the Subflow interaction element.

My approach and the sample flows I’m providing are by no means perfect for every use case, and I make lots of room for the possibility that someone else has a better way of using Flows for file uploads. But that’s what makes Salesforce so great – there’s almost always more than one way to do something.


Some Limits

Record-Triggered Flows. You can’t use Record-Triggered Flows for ContentDocument, ContentVersion, and ContentDocumentLinks (womp womp), limiting the automation that’s possible when created, updated, and deleted. A workaround for ContentVersion might be to use a trigger to publish a Platform Event, and then use a Platform Event-Triggered Flow.

Standard File Upload Component. The standard File Upload screen component isn’t perfect – but don’t let the search for a perfect component become the enemy of a “good enough” component. It’s still wicked awesome. But here are some of the shortcomings to plan around:

  • Once a file is uploaded and you click “Done” to close the modal, the File Upload component looks just as empty as it did when you began. It can be difficult for a user to know the file was truly uploaded and they can proceed. But see Max Flow #1 (above) for a great alternative.
  • Standard component can be used multiple times in a row, and will create files every time. So if your plan is to process each uploaded file in a special ways, like in Use Cases #4 or 6, your loops might have more records in them than you realize.

DML/SOQL Limitations. Several of these sample flows rely on loops to get or update files that have been uploaded. There are limits in place on the number of DML and SOQL statements – the Create, Delete, Get, and Update elements in your flow – that can be executed. This can be a problem in loops, where you may not be able to predict the number of times the loop will iterate. For loops that include DML and SOQL statements and will iterate over a lot of values, you’ll probably need to build loops that assign updated records to a collection, so that you can bulk update all the looped records at once. For some of the above use cases, you may need to first get records based on a collection of record Ids – which can use this solution from Salesforce Binge to easily create that query.

Querying for Multiple Files. It doesn’t really apply in these scenarios, but you should know that directly querying ContentDocument and ContentVersion objects won’t return all the files a user has access to – you can’t blindly query these objects, you have to have a certain ContentDocument Id, which means (in most cases) you’re returning a single file. For the most part, you will need to be querying ContentDocumentLink for any query returning more than one Content Document.


Please Help These Great Ideas!

There are some great ideas for enhancing Flow-based file uploads in the Salesforce IdeaExchange. Please consider upvoting for the following:


For Maximum Flow, Turn the Tap On Full

There is a whole wide world out there – lots of ways to use Flow to do some terrific things with files. Try out these components to extend what you need to do in Flow.

Improved File Upload in Flow Screens

This is a wonderful alternative to the standard File Upload screen component for single and multiple file uploads. Two key differences:

  • Shows the user a list of the uploaded files
  • Can be marked as a required input, which can simplify your overall flow

I can’t overstate how terrific this component is. To learn more and install:  https://unofficialsf.com/from-josh-dayment-improved-file-upload-in-flow-screens/

Import Data from CSV Files Using Salesforce Flow

Once a CSV file has been uploaded, use this Flow Action to convert the CSV file into Salesforce records.

To learn more and install:  https://unofficialsf.com/narender-singh-brings-csv-imports-to-flow/


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: