Talk to Expert

Understanding VersionData Binary Download in Salesforce

Share this Article:

Download Salesforce files programmatically
AI-Powered Reading

Explore This Article with AI

Get an instant summary, ask questions, or go deeper-open this page in your favourite AI tool in one click.

If you want to Download Salesforce files programmatically, understanding the Content Version and Version Data field is essential. If you have ever tried to download file content directly from Salesforce using SOQL or the REST API, understanding how the Salesforce Content Version Version Data field download binary process works is absolutely essential.

Thank you for reading this post, don't forget to subscribe!

How to Download Salesforce Files Programmatically Using REST API

The Version Data field is a Base64-encoded binary field on the Content Version object. It stores the actual raw content of a file – whether that is a PDF contract, a JPEG image, an Excel spreadsheet, or any other supported file format.

Unlike most Salesforce fields that return plain text or numbers, VersionData returns binary data encoded in Base64 format. This means you cannot simply read it like a string — you need to decode it properly to retrieve the original file in its usable format.

How to Query VersionData Using SOQL

You can query the VersionData field directly through SOQL, but there is an important limitation – VersionData is a blob field and cannot be retrieved in bulk queries via the standard SOQL API. Instead, you must query it record by record using the Salesforce REST API or Apex.

Here is a basic SOQL query to retrieve the ContentVersion record first:

soql

SELECT Id, Title, FileType, ContentSize, VersionData

FROM ContentVersion

WHERE IsLatest = TRUE

AND Title = ‘YourFileName’

LIMIT 1

Once you have the ContentVersion ID, you can download the binary content using the Salesforce REST API endpoint:

/services/data/vX.X/sobjects/ContentVersion/{Id}/VersionData

This endpoint returns the raw binary file content directly, which you can then save locally or process further.

Downloading Binary Files via the REST API

To perform a Salesforce Content Version Version Data binary download, you need an authenticated REST API call with the correct headers. Developers often use the REST API to Download Salesforce files programmatically because it provides direct access to binary content.

Here is an example using a standard HTTP request:

GET /services/data/v59.0/sobjects/ContentVersion/{ContentVersionId}/VersionData

Authorisation: Bearer {AccessToken}

The response body contains the raw binary content of the file. You can write this directly to disk using your preferred language – Python, Node.js, Java, or any other HTTP-capable environment.

One key point – always check the FileType field before processing the binary response so your application knows how to handle the file correctly.

ity_Doc.pdf), follow this 2026 workflow:

Using VersionData in Apex

In Apex, you can access VersionData as a Blob data type. Here is a simple example:

apex

ContentVersion cv = [SELECT Id, Title, VersionData, FileType

FROM ContentVersion

WHERE IsLatest = TRUE

LIMIT 1];

Blob fileContent = cv.Version Data;

String base64Content = EncodingUtil.base64Encode(file Content);

This converts the binary blob into a Base64 string, which you can then pass to an external system, store in a custom field, or use within an integration.

Common Errors When Working With Version Data

The most frequent issue developers encounter is attempting to query Version Data across multiple records in a single SOQL call. Salesforce restricts this because returning binary data for many records simultaneously would exceed heap size limits. Always query Version Data one record at a time.

Another common mistake is forgetting to authenticate properly before calling the REST API endpoint. Ensure your access token is valid and your connected app has the correct permissions for file access.

In Summary

The Salesforce Content Version Version Data field is the gateway to programmatic file access in Salesforce. Whether you are building a file migration tool, an integration pipeline, or a custom export solution, understanding how to query and download binary content correctly is a foundational skill. For large-scale exports without writing custom API code, tools like Files Downloader handle the entire Version Data binary download process automatically – saving development time while keeping all file processing securely inside your Salesforce org.

[Book a Free Demo] | [View Pricing] | [Install on AppExchange]

Table of Contents

The VersionData field in Salesforce ContentVersion is a Base64-encoded binary field that stores the actual raw content of a file. This includes PDFs, JPEG images, Excel spreadsheets, and any other supported Salesforce file format. Unlike standard text fields, VersionData returns binary data that must be properly decoded before the file can be used or exported in its original format.

To perform a Salesforce ContentVersion VersionData binary download, you need to use the Salesforce REST API with an authenticated request. First query ContentVersion using SOQL to retrieve the record Id, then call the REST API endpoint /services/data/vX.X/sobjects/ContentVersion/{Id}/VersionData with a valid Bearer token. The response returns the raw binary file content, which can be saved locally or processed using any HTTP-capable language such as Python, Node.js, or Java.

No - Salesforce restricts bulk retrieval of the VersionData field in a single SOQL query. Because VersionData is a blob field containing binary content, returning it for multiple records simultaneously would exceed Salesforce heap size limits. The recommended approach is to query ContentVersion records first without VersionData, collect the record IDs, and then download the binary content one record at a time using the Salesforce REST API.

In Apex, the VersionData field is accessed as a Blob data type. You can query a ContentVersion record, assign the VersionData to a Blob variable, and then use EncodingUtil.base64Encode() to convert the binary content into a Base64 string. This Base64 string can then be passed to an external system, stored in a custom field, or used within a Salesforce integration or automation flow.

The two most common errors when working with Salesforce ContentVersion VersionData binary downloads are attempting to retrieve VersionData across multiple records in a single SOQL query - which causes heap size limit errors - and failing to authenticate properly before calling the REST API endpoint. Always ensure your OAuth access token is valid and that your Salesforce connected app has the correct file access permissions before attempting a VersionData binary download.

Setup → Quick Find → Salesforce Files → General Settings → Edit → Check "Skip triggers execution and validation rules on asset files" → Save