/template-sfdc2localdisk-file-download

A simple example showing how to extract a file from Salesforce using MuleSoft Anypoint

MIT LicenseMIT

template-sfdc2localdisk-file-download

A simple example showing how to extract (or download) a file from Salesforce using the MuleSoft Anypoint 4.3 runtime and save it to local disk

Background

I wanted to build another MuleSoft Anypoint integration that would allow me to analyze files already uploaded to Salesforce, and found it was suprisingly difficult to find an example of how to get a file. Therefore I created this simple project to show how you can extract a file out of Salesforce and save it to disk. No more.

Get Started

  1. Open the project in AnyPoint Studio 7.7: (probably runs in later versions as well)

  1. Set the values of the Global Elements to valid Salesforce login and File Locations (Salesforce login requires read access to that file)

  2. Find the ContentVersionID (see Process below)

  3. Debug or run, and invoke it from your favorite REST tool:

Process

This is just a description of the process to do this from begining to end:

Step 1) Your file will have a Salesforce ID that refers to the ContentDocument. You want the row in the ContentVersion Object that refers to this ID as well as IsLatest (or you may select an older version by mistake). An easy way is just look at the URL in Salesforce's file details, but you could SOQL it instead:

Step 2) Use your SOQL powers to find the Content Version ID of the file you are looking for:

Step 3) Configure a Salesforce Retrieve step, and pass the file's ContentVersionId in to extract the data out of the ContentVersion object.

Step 4) Extract the file data (as a Base64 string) and convert it to Binary

Last thoughts

  1. A good developer will use streaming to get files.... but I'm not too interested in that for this exercise. Salesforce has org based limits on file size, but this method can download a 20MB PDF fast enough.

  2. Downloading this way doesn't increment the file's download count.

  3. This Flow could be shortened a great deal, however this way is easier to examine each step and output the log.

  4. You could return the file in the service itself, rather than save to local disk. Go for it.

  5. This project doesn't have a lot of good practices that Anypoint should have, such as error handling, input validation, etc. It shouldn't be used for Production. The intention is just to show how to get a file.