How to Upload a Document in Salesforce Files Using Rest API
Files are one of the most powerful ways to manage the content documents in Salesforce. It also refers to a content management system. We have many options such as attachments, documents, Salesforce CRM content to upload a file in Salesforce classic but they are not fully supported or at all in Lightning experience. So it is recommended to forget about the familiar Notes and attachments related list and continue with the files related list in the future. In this article, we are going to explain about differences between files and attachments, file sharing settings, and how to upload a document in files using REST API and an example.
Comparing Files and Attachments in Salesforce:
The following are the differences between Files and attachments in Salesforce.
File Sharing Settings:
Files can be managed in Salesforce based on the following file sharing settings.
Uploading a document in Salesforce files using REST API
Sometimes there is a requirement to upload a document using REST API. So, the following are the three objects which are used to upload a document in Salesforce files and link it to multiple records.
Let’s have an in detailed knowledge of all the above objects.
Content Version:
This object stores the document information similar like Attachments. It contains the information such as content location, path on client, version data, and title.
Content Document:
It represents a document that has been uploaded to a library in Salesforce Files. There is no need to create this object and it automatically gets created when the content version is created.
Content Document Link:
This object will be used to share the files with users, records, groups etc. The same file can be linked to multiple records by using Content Document Link. It can also provide the facility to assign the users to access the file with certain permissions. It contains the information such as content document, linked entity, sharing type, visibility level.
Note: AllUsers & InternalUsers values apply to files posted on standard and custom object records, but not to users, groups, or content libraries.
Recommended by LinkedIn
Example:
Let’s see an example to create a document in the files section using postman.
Step 1: Create a Content Version
Step 2: Fetch the Content Document Id from Content Version.
By using the SOQL query, get the content document id from the content version by passing the content version id.
Step 3: Search with the content document id in Salesforce to display the created file.
From the below screenshot, we can see that the file has been created and by default it is shared with the created user.
Step 4: Assign the file to a record by using the content document link.
Here, we are linking the above-created file to an opportunity.
From the below screenshot, the file has been linked to an opportunity in Salesforce.
Conclusion:
In this way, we can create a document in Salesforce by using REST API. We sincerely hope that this was useful and any comment or feedback will be very helpful.
Below is the entire video of the process!
The original article and author here:
Software Engineer at GovTech
1yMausam Padhiyar You need convert the file to Base64 encoded format in order to input to "VersionData". While both Blob and Base64 are used to handle binary data, Blobs represents raw binary data whereas Base64 is a binary to a text encoding scheme so ofcourse Blob.valueOf won't work
Salesforce Developer | Technical Lead at Cognizant
1yHi, As a system admin user can able to post/upload file with /services/data/v45.0/sobjects/ContentVersion end URL, but as different user (Integration user) getting 404 error with below json response. [ { "errorCode": "NOT_FOUND", "message": "The requested resource does not exist" } ] do you know what access need to give?
Empowering Organizations with Streamlined Business Processes & Solutions
3yHow to populate VersionData field on ContentVersion object? File shows error when tried to open after creating ContentVersion record. I have used Blob.valueOf method.