Copy Email Attachments to Case Folders in external storage if Email-to-Case is activated
In this tutorial, we'll guide you on copying Case email attachments to corresponding Case record's connected folder in any external storage when you have Email-to-Case enabled in your Org using CloudFiles-Salesforce Integration. We will use Salesforce flows with CloudFiles flow actions to automatically copy Case email attachments into corresponding case folder whenever a new email is recieved.
By the end of this tutorial, you can expect to -
- Have Case folders created in external storage automatically whenever a new case record is created.
- If your Org has been enabled to create Case records through Email-to-Case, then copy the Case email attachments into corresponding case folder in external storage.
The GIF below shows the expected output.
The configurtion for this involves a few Salesforce flows with CloudFiles actions in them. In the sections below, we cover each of these Salesforce flows , the actions used in them 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.
Set up Email-to-Case to have Salesforce automatically create case records when email messages are sent to the configured support email addresses. If you haven't yet, you can follow this Set Up Email-to-Case Salesforce Help Doc.
To add the CloudFiles widget to the Case record page in Salesforce, first, navigate to any Case 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 canvas. 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 record-triggered flow to automatically create Case folder whenever a new Case is created.
This is how the final flow looks like -
To configure the record-triggered flow on the 'Case' Object, follow these detailed instructions:
- Create a new flow and select 'Record-Triggered' as the flow type.
- Specify the object as 'Case'.
- Choose the trigger option 'A record is created'.
- Optimize the flow for 'Actions and Related Records'.
- Check the 'Include Asynchronous Path' checkbox to enable the 'Run Asynchronously' path in the flow.
NOTE - In Record-triggered flows, make sure you run CloudFiles actions only in 'Run Asynchronously' path.
Refer to the GIF below for a visual understanding of this step -
In the 'Run Asynchronously' path add a Create Folder action to create Case folders at any desired location in connected external storage.
Once you add this action, it will ask you to fill in certain parameters.
- Library - This is the desired Cloud storage where you wish to create the folder. Input sharepoint for SharePoint, google for Google Drive, onedrive for OneDrive Personal & OneDrive for business, dropbox for Dropbox, box for Box, cloudfiles for AWS s3.
- Folder name - Input the Folder name you wish to give your created Folder. Here, we input the Case number {!$Record.CaseNumber}but you can also use a complex text formula if you wish to.
- The next two parameters i.e. Parent Folder ID and Drive ID help specify where the target folder should be created. E.g. in SharePoint, which site, drive and parent folder should this folder get created in. These parameters can be easily copied from within Salesforce using the 'Content Library' tab on CloudFiles.
- Parent Folder ID -This is the cloud drive generated ID of the folder where you want to create this folder in.
- Drive ID -This is important for google drive & sharepoint users only. These cloud storages offer multiple 'drives' where documents can be uploaded.
These parameters can be copied by accessing your Cloud storage using CloudFiles from within Salesforce. Follow these steps -
- Click on App Launcher on top left. Search for the “CloudFiles” App.
- In the CloudFiles App, select the Content Library Tab.
- Go to your preferred cloud storage. Go to the target folder where you would like to place the created folders in.
- Click on ‘View metadata’ button as shown below. A dialog box opens up which shows all the IDs. Copy the ID from here.
Refer to the GIF below to get a visual understanding of this step.
For a detailed description of the Create Folder action, refer to the Create Folder detailed guide. This flow action returns various values related to the newly created Case folder. We will use the newly created folder’s details in the next step to attach it to the Case, so that the folder is visible in the Case record page's CloudFiles widget.
Add a Create Attachment action to attach the created Case folder to Case record. Attaching (or associating) our newly created Case folder is important since this will enable us to see and operate on the folder contents right from the Case record.
Once you add this action, it will ask you to fill in certain parameters.“Drive ID” and “Library” parameters will be same as those input in Create Folder action. Add the remaining parameters as given below - Add the parameters as given below -
- Resource Id - Input the 'resource Id' of the folder to be attached. se the output of “Create Case Folder” action as 'Create Case Folder' > id (resource id)
- Resource Type - Input the Resorce Type of the resource to be attached. Here, simply enter folder.
- Salesforce Record Id: ID of the Salesforce Record Case onto which the resource is to be attached. Here, input the flow triggering Case Record Id i.e. {!$Record.Id}
Refer to the GIF below to get a visual understanding of this step.
Once the Flow is activated, for every new case record created, this flow automatically creates a Case folder in the external storage at the desired location and attaches it to the Case record.
We will create a record-triggered flow on Email message object to automatically copy Email attachments into corresponding Case folder.
This is how the final flow looks like -
To configure the record-triggered flow on the 'Email Message' Object, follow these detailed instructions:
- Create a new flow and select 'Record-Triggered' as the flow type.
- Specify the object as 'Email Message'.
- Choose the trigger option 'A record is created'.
- Set the entry conditions, to trigger the flow only when the Email Message has files attached to it. For this, select 'Condition Requirements' as 'All Conditions Are Met (AND). Set the single condition, with field as 'HasAttachment', operator as 'Equals' and Value as 'TRUE'.
- Optimize the flow for 'Actions and Related Records'.
- Check the 'Include Asynchronous Path' checkbox to enable the 'Run Asynchronously' path in the flow.
NOTE - In Record-triggered flows, make sure you run CloudFiles actions only in 'Run Asynchronously' path.
Refer to the GIF below to get a visual understanding of this step.
In the 'Run Asynchronously' path add a 'Get Records' element to output all the 'Content Document Link' records linked to the 'Email Message' that triggered the flow.
Refer to the GIF below to get a visual understanding of this step.
This element outputs a 'Content Document Link Record Collection' with all the records associated with the flow triggering Email Message record. The output of this element shall be used in subsequent elements of the flow.
Add a 'CloudFiles: Delay' Apex action into the flow, positioned after the 'Get Records' element and before retrieving details of the corresponding Case folder in the subsequent action. The 'CloudFiles: Delay' Apex action functions as a 'pause for a given time' element before the flow proceeds to execute the next action or element.
The execution of "Flow 1", responsible for Case Folder creation and attachment, may encounter delays. To prevent flow errors when fetching Case folder details before the completion of "Flow 1", this delay element is incorporated into the flow.
Once you add the 'CloudFiles: Delay' Apex action, it will ask you to fill in a single parameter.
Delay(milli seconds) - The amount of time the pause acts for in milliseconds.
Refer to the GIF below to get a visual understanding of this step.
NOTE - Avoid setting a long pause duration as it may result in flow timeouts and errors. For this specific flow, a delay of 10,000-15,000 milliseconds should suffice.
Add a Get Connected Folder Apex action to output the details of corresponding Case record's folder.
Once you add the action, it will ask you to fill in a single parameter.
ParentId - Input the Salesforce Record Id whose connected folder is to fetched. Here, input the flow triggering Email Message record's Case Id as 'Record' > 'Case ID' i.e. {!$Record.ParentId}
Refer to the GIF below to get a visual understanding of this step.
In case the Salesforce Record has got a single folder attached to it, then Get Connected Folder outputs the attached folder details.
Add a 'Loop' element and set it to loop through the 'Content Document Link Record Collection' output by the 'Get Email Attachments' element. For each 'Content Document Link' in the loop, with an 'Assignment' element, add its corresponding 'Content Document ID' to a text collection variable. After the end of the loop, the text collection variable will contain all the Content Document IDs of the email attachments.
Refer to the GIF below to get a visual understanding of this step.
Add a Copy Salesforce Files Apex action to Copy the Email Attachments to the fetched Case folder. Once you add this action, it will ask you to fill in certain parameters. Add the parameters as given below -
- Content Document Ids - Input the Content Document Id Collection which are to be copied. Here, input the Content Document Id Collection gathered by the Loop.
- DriveId - Input the copy destination's Drive Id (Only for Sharepoint and Google Drive) Here, input the 'Drive Id' of Case Folder output by 'Get Case Folder' action.
- Folder Id - Input the copy destination's Folder Id. Here, input the 'Folder Id' of Case Folder output by 'Get Case Folder' action.
- Library - Input he copy destination's Cloud storage. Input sharepoint for SharePoint, google for Google Drive, onedrive for OneDrive Personal & OneDrive for business, dropbox for Dropbox, box for Box, cloudfiles for AWS s3. You can even use the outputs of the 'Get Case Folder' action. Here, input the Cloud Storage or use 'Library' output by 'Get Case Folder' action.
- Salesforce Record Id (Optional) - Optionally input the Record Id from which the files will be copied. Here, optionally input the Email Message Id from which the files will be copied.
NOTE - The 'Content Document Ids' parameter only accepts a text collection as a valid input. Even if you have only one Content Document Id, you must input it as a text collection.
Refer to the GIF below to get a visual understanding of this step.
Once the Flow is activated, for every new Email received , this flow automatically copies the email attachments to Case folder attached to respective Case record.
Now that everything is setup, lets test our flows. Follow the steps below to see this automation in action.
- To create a case, send an Email to Email address used in Email-to-Case setup.
- Attach a few files to this email. These can be any type or size of files.
- Open the created case. It may take upto a minute for the case to get created by Email-to-Case functionality.
- Within the CloudFiles widget on Case record page:
- Find an automatically created Case folder in the configured external storage.
- Find all the Email attachments copied into the Case Folder.
Flow execution and folder creation may take some time (a few seconds to a minute). If you don't see any changes, try refreshing the CloudFiles widget a couple of times.
Congratulations! You've successfully configured flows to automatically create Case folders for new Case records and copy Case Email Attachments to their respective Case folder in external storage.