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
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.
- Open the project in AnyPoint Studio 7.7: (probably runs in later versions as well)
-
Set the values of the Global Elements to valid Salesforce login and File Locations (Salesforce login requires read access to that file)
-
Find the ContentVersionID (see Process below)
-
Debug or run, and invoke it from your favorite REST tool:
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
-
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.
-
Downloading this way doesn't increment the file's download count.
-
This Flow could be shortened a great deal, however this way is easier to examine each step and output the log.
-
You could return the file in the service itself, rather than save to local disk. Go for it.
-
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.