Salesforce Attachments are wonderful things. Not only are there multiple ways to store them (Classic and File), there are also multiple APIs to read them and….

  • Neither the SOAP or REST  Salesforce APIs can read every attachment.

Note: This article actually applies to any Salesforce table with a BASE64 column.  I picked Attachments because it is one everyone knows about.  Yes, I know that Attachments have a size limit well under 2GB, but some tables with a BASE64 column do allow 2GB.  For simplicity, this article will pretend that Attachment also support 2GB files.

How can this be true? Let’s look at the REST and SOAP APIs using a short quiz.

I want to read Attachments where some of them might be in the recycle bin.
What API should I choose?

Click for Answer!

I want to read Attachments where some of them may be really big.
What API should I choose?

Click for Answer!

My Attachment reader has to run in 200MB of memory.
What API should I choose?

Click for Answer!

I have millions of Attachments to read over the weekend.
What API should I choose?

Click for Answer!

Let’s look a bit closer at how to make Attachment reading fast via the REST API.
Here is a simplified version of the algorithm we use.

Copy to Clipboard

Before you write to us and say “This code won’t compile” — It is not intended to because it is just pseudo code.

The real code we use to read millions of Attachments is much more difficult. In practice:

  • Code has to switch to SOAP when REST fails (and switch back afterwards).
  • Fifty threads will likely swamp your internet connection.  We seen as few as 10 concurrent downloads of large attachments swamp a 1GB connection.
  • If you push too much parallelism Salesforce may decide you are attempting an attack on their network and shut you out for a few minutes.
  • Requests to Salesforce do not always work because the internet is just not that reliable. You will need to recover from communication errors.
  • Salesforce has been known to invalidate sessions and send INVALID_QUERY_LOCATOR errors for long running sessions. You will need to recover and reconnect.