Automatically moving files from Salesforce storage to external storage
In this tutorial, we will demonstrate how to automatically move files uploaded to Salesforce storage to an external storage system while deleting the originally uploaded files using Salesforce-CloudFiles integration. We will utilize CloudFiles flow actions to facilitate the file movement process. Files in Salesforce can come from a variety of source, for example - from your document generation tool, e-signature tool, email to case, or manual uploads by your users.
By the end of this tutorial, you can expect to -
- Copy the files uploaded in Salesforce storage to Account’s parent folder in external storage automatically
- Automatically delete the original file stored in Salesforce storage.
The GIF below shows the expected output. If you have a basic grasp of these concepts, you can expect to finish the tutorial in under 10 minutes.
Our final setup involves a number of flow actions added to a Salesforce flow as shown below. In the sections below, we cover each of these actions and how to configure them.
Before you start with the set-up, please ensure these steps are completed.
CloudFiles is available on the Salesforce AppExchange and can be installed using the standard app installation process. Check out Installation Guide to know more.
All automations in CloudFiles are activated through Salesforce Flow Builder, utilizing account-wide authentication for external storage libraries through a service (admin) user. To connect your service user, access the setup, click on the external storage of choice and log into your account. For detailed steps, follow Automations or refer to the GIF below for a clearer understanding.
To add the CloudFiles widget to the Account record in Salesforce, first, navigate to the Account page and click on the "Edit Page" button. Once in the Lightning Builder, insert a new tab and drag and drop the "CloudFiles" widget onto the page. Save the changes and exit the builder. Refresh the Salesforce record page, and the widget should now be visible, allowing easy access to view files and folders within Salesforce.
We will create a platform-triggered flow that will fire when a 'CloudFiles Event' message is received. Refer to the GIF below for a visual understanding of this step -
We will add two decision outcomes here: First, to copy the files from Salesforce storage to external storage, and second, to delete the original files from the Salesforce storage. The details are discussed below, one by one.
‘CloudFiles event’ messages can be generated for various events, we will first filter out the messages only for the event when a file is attached to the 'Notes & Attachment' of an account. We will proceed to retrieve the details of the Account’s connected folder and then copy the files from Salesforce storage to that specific account folder.
Start by adding a new “Decision’ element. Add an outcome for file uploads in Salesforce storage. It will ask you to fill in certain parameters. We will add 2 resources as given below
1 - $Record > Object Type equals Account
2 - $Record > Type equals “salesforce-file-attached”
This is how the Outcome Detail looks like -
Refer to the GIF below to have a visual understanding of this step -
In order to get the Account ID of the account for which the file upload has happened, we will use two CloudFiles action - “Get Events Details’ and "Get Connected folder".
To retrieve event details, we'll use the ''Get Event Details" action, which provides various information about the event, including the Account record ID. This ID will be used in the next action. In order to do so, add this action and select 'Record' in the event dropdown menu.
This is how the action looks like -
Previous action will return various outputs. In order to get the folder ID of the account folder, add ‘Get Connected folder’. In the Parent ID field, select ‘SalesforceFiles’ > ‘ParentId’.
Refer to the GIF below to have a visual understanding of this step -
Once we have retrieved the Account Parent folder details, we will copy the files from Salesforce to the external storage.
We will use ‘Copy Salesforce Files’ action to copy the file from Salesforce storage to external storage like SharePoint, Google Drive etc. Once you add this action, it will ask you to fill in certain parameters as shown below
1 - Content Document Ids - Salesforce ID of the uploaded file. Select SalesforceFiles > ContentDocumentIds from the output of previous ‘Get Event Details’ action
2 - Drive Id - Drive Id in external storage where file should be copied. Select driveId from the output of ‘Get Connected folder’ action.
3 - Folder Id - Id of the folder in external storage where the file should be copied to. Select driveId from the output of ‘Get Connected folder’ action. Select library from the output of ‘Get Connected folder’ action.
4 - Library - External storage type.
5 - Salesforce Record Id - ID of the Salesforce record where the original Salesforce file is attached. Select ‘SalesforceFiles’ > ‘ParentId’ from the output of ‘Get Event Details’ action.
This is how the action looks like -
Refer to the GIF below to have a visual understanding of this step -
Now that we have copied the files from Salesforce storage to external storage, we will delete the originally uploaded files from the salesforce storage once the copy process is completed. In order to do so, we will created another branch in the previous decision action as discussed in the next steps.
Start by adding a new Outcome to previous Decision element. This is to determine if the file is copied from the Salesforce storage to external storage. It will ask you to fill in certain parameters. We will add 1 resource here - $Record > Type Equals salesforce-file-copied
This is how the Outcome Detail looks like -
Refer to the GIF below to have a visual understanding of this step -
To retrieve copied file event details, we'll use the '' action again to get the Content Version ID. This ID will be used in the next action. In order to do so, add this action and select 'Record' in the event dropdown menu.
This is how the action looks like -
To get the original file uploaded in Salesforce storage, we will retrieve the Content Version, which represents a specific version of a document uploaded to a Salesforce storage. This is where the actual content of a file is stored. To do so, we will add the 'Get Records' element.
We will add the object as ‘Content Version’ and we’ll add 1 resource below - ContentVersion ID equals SalesforceFileCopied > ContentVersionId (you will get this from the output of previous Get Event Details action).
This is how it looks like -
Refer to the GIF below to have a visual understanding of this step -
Once we have retrieved the content version, we will proceed to delete the original document. For this we will use Content document which has been uploaded to Salesforce Storage. Add 'Delete Records' element.
Select object as ‘Content Document’ and we’ll add 1 resource below - ContentDocument ID equals ContentDocumentId > (you will get this from the output of previous Get Records action).
This is how the action looks like -
Refer to the GIF below to have a visual understanding of this step -
That’s it. Save and activate the flow!
Now that everything is setup, lets test our flow. Follow the steps below to move files from Salesforce storage to Account folder in external storage.
- Create a new Account record in Salesforce or go to an existing Account record and upload a file in Notes & Attachment.
- Navigate to ‘Files’ tab. The flow may take a few seconds to run so click on the refresh icon on top right of the widget.
- You should be able to see the uploaded file.
- Now navigate back to Notes & attachment. You should be able to see that the original file has been deleted.
Congratulations! You've successfully moved files from Salesforce storage to Account folder in external storage.