> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/salesforce/open-save/saving-document-in-salesforce.md).

# Saving a Document in Salesforce

Learn how to manage documents for Salesforce with ContentVersion object and Apex code snippets. Implement document versioning and viewing with WebViewer. Full code sample available on Github. Salesfor

When saving documents in Salesforce, there are two approaches, depending on the file size. For smaller documents, you can store them directly in Salesforce using the standard `ContentVersion` object, allowing for easy retrieval and integration within Salesforce records. However, for larger files, Salesforce [governor limits](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm)(6MB/12MB async) and file size restrictions prevent direct storage.

We recommend using an External Storage Solution, where the document is uploaded to an external system (e.g., AWS S3, Azure Blob Storage), and only a reference (such as a URL or identifier) is stored in Salesforce. This ensures large documents can still be accessed through Salesforce without hitting platform limits, providing flexibility for both standard and large file use cases.

## Save a document directly in Salesforce

You can store small files directly in Salesforce because they stay within the platform’s storage and memory limits.

### Save a small file to Salesforce

{% hint style="info" %}
Note: We recommend a review of the [Overview](/salesforce/get-started/readme.md) page to learn how to correctly use a `config.js` before getting started using Webviewer.
{% endhint %}

The process of saving a document to Salesforce begins with WebViewer, where you:

1. Trigger a save action embedded in the UI. This initiates a function that prepares the document data payload.
2. The payload is sent using `postMessage` to a Salesforce Lightning Web Component (LWC) that listens for incoming messages.
3. The LWC captures the payload and passes it to an Apex controller, which handles the server-side logic.
4. The Apex controller creates a `ContentVersion` object using the received data and inserts it into the Salesforce database.
5. The document is stored securely within the Salesforce content management system, making it accessible for future use or reference.

### How save works in Salesforce

When you save a document to Salesforce, a function, message, and method work to save the document within Salesforce.

#### WebViewerSaveDocument

The `saveDocument` function prepares a document to be saved in Salesforce through the following process:

1. Collect and encode the file data.
2. Retrieve the document's file size, type, and name, renaming image files to use a .pdf extension.
3. Export annotations and fetch the document data with the annotations included. The binary data converts into a Base64-encoded string suitable for transmission.
4. A payload is created with metadata and content and, if the file size is under Salesforce's limit, it posts the data to a Lightning Web Component (LWC) via `postMessage`; otherwise, it triggers a fallback download.

#### Receive Message

When a trusted message of type `"SAVE_DOCUMENT"` is received, the following process happens:

1. It extracts the document payload and calls the `saveDocument` Apex method, passing the document data and related record ID.
2. On successful save, it notifies the iframe that the document was saved. If an error occurs, it sends an error message back to the iframe, logs the error, and shows a user notification with the error details.

#### Apex Save Document

The `saveDocument` method in the Apex controller handles saving an annotated document into Salesforce by using the following process:

1. Parse the incoming JSON payload into a custom `PDFTron_ContentVersionPayload` object.
2. Retrieve the original `ContentVersion` record using the provided ID (cvId) to find the associated `ContentDocumentId`.
3. Using the ID, construct a new `ContentVersion` record that includes the annotated file (decoded from Base64), and insert it as a new version of the existing document.
4. If the document is newly created (i.e., not associated with an existing `ContentDocumentId`) and a related record ID is provided, create a `ContentDocumentLink` to associate the uploaded document with a specific Salesforce record.
5. The method returns the ID of the newly inserted `ContentVersion` or, throws an AuraHandledException if an error occurs.

## Save a document outside of Salesforce

Large files must be stored externally because they exceed Salesforce’s processing and storage limits, while smaller files can be stored directly in Salesforce without issue since they stay within the platform’s governor limits for file size and memory usage. Leveraging external storage provides more flexibility for handling large files while still integrating with Salesforce workflows.

### Save a large file to Salesforce

When you want to save a larger document to Salesforce, you must save it to an external storage solution like AWS S3, then relate the file to its Salesforce record.

Our Salesforce integration supports an external storage solution. Instead of uploading the full PDF file into Salesforce, you can:

1. Configure your environment so that a Lightning Web Component (LWC) sends the document directly to an external storage system (such as AWS S3, Azure Blob Storage, or similar).
2. The external service returns a Salesforce record reference (such as a URL or file ID), which is stored in Salesforce for future access or display. This allows you to handle large files efficiently while still maintaining a link to them within the Salesforce platform.

This hybrid approach combines Salesforce’s strengths in record-keeping and workflow with the scalability of external storage systems—ensuring performance, compliance, and usability for flexible document workflows.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/salesforce/open-save/saving-document-in-salesforce.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
