Uploading files in Salesforce is a common requirement for businesses. Documents like invoices, receipts, catalogs, agreements, etc. are often shared with prospects, clients, and customers. And many times, the built-in file upload feature isn’t enough. In such cases, using the REST API provides a programmatic and efficient method for file uploads. So our step-by-step guide will show you exactly how to do it by leveraging the Salesforce file upload API.
Key Components of Salesforce Files
Salesforce Files is a modern file management solution within the Salesforce ecosystem. It’s designed to store, share, and manage files in a secure and organized way. It replaces the older “Attachments” functionality, providing enhanced capabilities and better integration with Salesforce’s collaborative and automation tools.
Key components include:
- ContentVersion: Represents a version of a file. Each time a file is updated, a new ContentVersion record is created. It contains information such as:
- ContentLocation: Represents the origin of the document. Valid values are:
i. “S”: S-Document is located within the Salesforce.
ii. “E”: E-Document is located outside the Salesforce.
iii. “L”: L-Document is located on a social network and accessed via Social Customer Service. - PathOnClient: Represents the path of the document. You must specify the complete path, including the path extension, for the document to be visible in the Preview tab.
- VersionData: It contains the file content.
- Title: It represents the name of the file.
- ContentLocation: Represents the origin of the document. Valid values are:
- ContentDocument: Acts as the reference for the file. A single ContentDocument can have multiple versions.
- ContentDocumentLink: Connects a ContentDocument to one or more Salesforce records (e.g., accounts, opportunities). It contains the following key fields:
- ContentDocumentId: Refers to the file (ContentDocument) being shared.
- LinkedEntityId: Represents the ID of the record with which the file is associated.
- ShareType: Indicates the level of access granted, such as Viewer, Collaborator, or Inferred permission based on record sharing.
- Visibility: Defines who can view the file. Possible values are:
i. AllUsers: Available to all users with permission to view the file.
ii. InternalUsers: Restricted to internal users only.
iii. SharedUsers: Limited to users who have access to the record or feed where the file is shared.
How to Upload Documents in Salesforce Files Using REST API?
Now let’s go through the steps to upload Salesforce Files using REST API.
Watch the video below and follow the step-by-step guide to upload Salesforce Files successfully.
Here’s a detailed breakdown of all the steps involved in uploading files with REST API.
Step 1: Connect Salesforce to Postman
We’ll use Postman as the third-party tool for interacting with the Salesforce file upload API.
Check this guide for the detailed process: Connecting Salesforce with Postman
Step 2: Uploading the file to Salesforce
Use the POST Request for ContentVersion API to upload the file as shown in the image below.
- In the JSON request, enter appropriate values for “Title”, “PathOnClient”, “ContentLocation”, and “VersionData”.
- For this example, the title of our file is “Casings and Instruments”. Content location is inside Salesforce and the path for the PDF file is specified. Finally, you can see the encrypted version data, which represents the content of the document.
- Once done, click on Send. If the request is successful, you should get a response as follows, indicating the file is uploaded in Salesforce.
Step 3: Fetch the ContentDocumentID
In the above response, copy the ContentVersion “id” value. Next, run the SOQL Query (GET request) with SELECT syntax as shown, and paste the copied ID at the end. Then click on Send.
The response request will give you the ContentDocumentID value as shown below.
Step 4: View the Uploaded File
Copy the ContentDocumentID and search for it in Salesforce. It should show up in the Files section, and you can check the preview.
Step 5: Assign the Uploaded File to a Salesforce Record
Next, to assign this file to a specific record (like an account or opportunity) in Salesforce, we’ll use the POST request with ContentDocumentLink API.
In the body of the JSON request, you’ll have to fill in details for the following objects:
- ContentDocumentID: Paste the ContentDocumentID you fetched in the earlier steps.
- LinkedEntityID: Paste the ID of the Opportunity to which you want to assign this file.
- Visibility: Choose a suitable visibility level.
Then click on Send.
When you get a success response, the file will be assigned to the opportunity. So go back and check the same in the CRM.
Following these steps, we can create a document in Salesforce by using REST API.
Salesforce Files vs Attachments – Know the Difference
Attachments are an older method for storing files in Salesforce. And while they remain functional in the CRM, they are not recommended for new implementations.
Salesforce Files, the newer system, offers superior features like version control and enhanced sharing options.
Users must be cautious to ensure their file uploads are categorized as Files, not Attachments, especially when managing legacy records or migrating data.
Why use REST API to upload files in Salesforce?
When sales reps and Salesforce users need to upload single files, the native upload feature in the CRM is enough.
But in the following scenarios, you’ll need REST APIs for file uploads:
Batch processing of bulk uploads
REST API supports bulk operations, so you can upload multiple files automatically. It also enables dynamic uploads from users in real-time scenarios, such as uploading attachments to cases, leads, or custom objects.
Automate file uploads with APIs
Manual file uploads can be time-consuming and prone to errors. Utilizing Salesforce’s APIs, you can automate file uploads as part of your workflows. Further, you can trigger uploads by specific events, saving time and ensuring consistency.
Integrate with Other Systems
Businesses using Salesforce usually integrate the CRM with other systems such as ERP or warehouse management software. REST API lets you programmatically upload files from these systems directly to Salesforce. It eliminates manual intervention while ensuring all records stay in sync.
Centralized File Management
With Salesforce file upload API, you can enforce rules for organizing and linking files. For instance, you can automatically attach files to the correct records, assign them to specific folders, or apply metadata tags, ensuring a more organized file management system.
Error Handling and Validation
REST API provides detailed responses for each operation including error codes and messages. It also gives you greater control over the upload process, facilitating error handling for issues like file size limits, unsupported formats, or record associations.
These features and scenarios are particularly useful for:
- Salesforce developers who want to upload documents programmatically using Salesforce’s REST API.
- System administrators who may need to configure or set up document uploads to Salesforce through API calls, especially when automating internal workflows.
- System Integrators who need to connect various systems (like ERP) with Salesforce for different processes, including document management.
- Salesforce users with custom requirements who want to extend Salesforce functionality and integrate documents in a way that Salesforce doesn’t provide natively.
What’s Next – Automate Salesforce File Management Further with APPSeCONNECT
REST API significantly improves the efficiency of file uploads in Salesforce. But even this process can be further streamlined and fully automated with an integration platform like APPSeCONNECT.
A common scenario for Salesforce users is syncing data between their CRM and ERP systems. This data can include customer details, inventory updates, pricebooks sync, and even files and documents. With APPSeCONNECT, you can take this automation a step further, seamlessly integrating multiple endpoints between Salesforce and your ERP.
If you’re looking for a simple solution to upload files more effectively, the steps outlined above will help you achieve that.
However, if you want to automate your entire business process by connecting Salesforce with your ERP and other systems, APPSeCONNECT is one of the most powerful and user-friendly iPaaS solutions.
To explore integration solutions for Salesforce, contact us or schedule a demo today!
Frequently Asked Questions (FAQs) about Salesforce File Upload API
1. How do I authenticate to the Salesforce REST API ?
While uploading files, you need to authenticate your request using OAuth 2.0. You need to create connected app in Salesforce and generate a token from the Salesforce UI. Once you get the credentials, you can use the token to access the resources.
2. What is the Salesforce file upload size limit?
The file size upload limit in Salesforce is 2 GB per file.
3. Can I upload multiple files simultaneously in Salesforce?
Yes, Salesforce supports Data Loader to upload multiple files at a time. You can also use Bulk API to upload a Zipped content which will be automatically extracted in the server. To do so, you must enable Zip file settings from the Settings Page.
4. How do I track the progress of file uploads in Salesforce?
Salesforce’s REST API doesn’t provide direct methods to track the progress of file uploads. You need to implement custom logic for it. You can use client-side code (JavaScript or libraries like Axios) to upload files in chunks, then track the percentage of completion. And another option is to use progress events during the upload process for real-time status updates.
5. How can I handle errors during file uploads?
To handle errors during file uploads in Salesforce, you should implement proper error checking and exception handling in your API calls. Common errors include authentication failures, file size limitations, and invalid file types. Ensure you capture and log any error responses from Salesforce’s API and retry the upload if needed. You can also notify the user about specific issues through the error message details returned by the API.