Tutorials

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.

What to Expect

By the end of this tutorial, you can expect to -

  1. Have Case folders created in external storage automatically whenever a new case record is created.
  2. 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.

Document image


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.

Pre-requisites

Before you start with the set-up, please ensure these steps are completed.

Install CloudFiles in Salesforce

CloudFiles is available on the Salesforce AppExchange and can be installed using the standard app installation process. Check out Installation guide to know more.

Document image


Connect your Cloud Storage to Salesforce using CloudFiles

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.

Document image


Set Up Email-to-Case

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.

Add CloudFiles Widget to Case Record Page

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.

Document image


Flow 1 - Create and Attach Case Folder on new Case creation

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 -

Document image


1 - Configure the Record Triggered Flow on 'Case' Object

To configure the record-triggered flow on the 'Case' Object, follow these detailed instructions:

  1. Create a new flow and select 'Record-Triggered' as the flow type.
  2. Specify the object as 'Case'.
  3. Choose the trigger option 'A record is created'.
  4. Optimize the flow for 'Actions and Related Records'.
  5. 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 -

Document image


2 - Create Case Folder using ‘Create Folder’ Action

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.

  1. 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.
  2. 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.
  3. 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.
    1. Parent Folder ID -This is the cloud drive generated ID of the folder where you want to create this folder in.
    2. 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 -

  1. Click on App Launcher on top left. Search for the “CloudFiles” App.
  2. In the CloudFiles App, select the Content Library Tab.
  3. Go to your preferred cloud storage. Go to the target folder where you would like to place the created folders in.
  4. 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.

Document image


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.

3 - Attach created Case Folder to Case record using ‘Create Attachment’ Action

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 -

  1. 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)
  2. Resource Type - Input the Resorce Type of the resource to be attached. Here, simply enter folder.
  3. 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.

Document image


4 - Save and Activate the flow.

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.

Flow 2 - Copy Case Email attachments to Case Folder on new Email message

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 -

Document image


1 - Configure the Record Triggered Flow on 'Email Message' Object

To configure the record-triggered flow on the 'Email Message' Object, follow these detailed instructions:

  1. Create a new flow and select 'Record-Triggered' as the flow type.
  2. Specify the object as 'Email Message'.
  3. Choose the trigger option 'A record is created'.
  4. 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'.
  5. Optimize the flow for 'Actions and Related Records'.
  6. 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.

Document image


2 - Get 'Content Document Link' records linked to the Email Message

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.

Document image


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.

3 - Add a pause element to ensure Case Folder creation

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.

Document image


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.

4 - Get Case Folder of the Email Message's corresponding Case

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.

Document image


In case the Salesforce Record has got a single folder attached to it, then Get Connected Folder outputs the attached folder details.

5 - Gather the Content Document IDs of all attachments to be copied.

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.

Document image


6 - Copy the Email Attachments' Content Documents to the Case Folder

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 -

  1. 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.
  2. 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.
  3. Folder Id - Input the copy destination's Folder Id. Here, input the 'Folder Id' of Case Folder output by 'Get Case Folder' action.
  4. 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.
  5. 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.

Document image


7 - Save and Activate the flow

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.

Test the Flows in action

Now that everything is setup, lets test our flows. Follow the steps below to see this automation in action.

  1. To create a case, send an Email to Email address used in Email-to-Case setup.
  2. Attach a few files to this email. These can be any type or size of files.
  3. Open the created case. It may take upto a minute for the case to get created by Email-to-Case functionality.
  4. Within the CloudFiles widget on Case record page:
    1. Find an automatically created Case folder in the configured external storage.
    2. 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.