As we know we can upload documents to salesforce directly but this is possible for document size up to 6 MB only. So, How to upload documents greater than 6 MB?
Here we are going to do it with the help of REST API :
UPLOADING DOCUMENTS USING REST API
There may occur a case where you need to upload a document using REST API. For instance, uploading a large document and linking it to multiple records. We will utilize the following objects to fulfill this requirement:
Content Version
Content Document
Content Document Link
You can get more details about each of the above-listed objects here.
Steps to upload document
Step 1 - Connect your postman with your org to get the access token.
To know more about how to connect postman with org: Click here
Response to the above API would come like this:
Choose the document you want to upload -> click on the button. 'Encode file to Base64'
Copy the encoded Base64.
Step 2 - Create a content Version
1. Enter endpoint <Your-org-instance>.my.salesforce.com/services/data/v54.0/sobjects/ContentVersion
2. Click on headers -> Select Key: Authorization and Value: Bearer <Access token Generated in Step 1>
Example: Bearer<space>00D5i000000l94I!ARkAQKy5Cp0YsNMEuGyRgKvon0.1iwfH30gXLKsqEvHKF3JrepJ4M7AB23klE4VddWLmt03cTtBNuiI1k28AD0Qp4TTZZ5gCa
3. Click on body -> Enter the below information
Title | Mention the document name. |
PathOnClient | Mention the complete path of the document. To see the document in the Preview tab, specify the complete path including the path extension. |
ContentLocation | It represents the origin of the document. |
VersionData | Provide encoded base64 generated in step 2. |
Click on the 'SEND' Button. You will get this as a response:
Step 3 - Fetch the Content Document Id from Content Version.
Use SOQL query to get the ContentDocumentId from content-version by passing content-version-id.
Method: GET
Content Version Id: Id received from step 3.
Endpoint:
<your-
instance>.my.salesforce.com/services/data/v54.0/query?q=SELECT+ContentDocumentId+FROM+ContentVersion+WHERE+Id+=+'<Content Version Id>'
In response we will get the ContentDocumentId.
You may recheck this document id by appending it after your instance like: <your-instance>.lightning.force.com/<ContentDocumentId>.
On search, you will be redirected to the document page in Salesforce
Step 4 - Assign the document to a record by using the content document link.
EndPoint: <Your-Instance>.my.salesforce.com/services/data/v54.0/sobjects/ContentDocumentLink
Method: POST
Set the body with the following attributes:
ContentDocumentid: - This is ContentdocumentId generated from step 4
LinkedEntityId:- Contain Id of records in which we have to attach the document.
Visibility: - Provide visibility of your document.
Response
Now, you have successfully linked the document to the record provided in LinkedEntityId.
Happy!! To help you add to your knowledge. You can leave a comment to help me understand how the blog helped you. If you need further assistance, please leave a comment or contact us. You can click on "Reach us" on the website and share the issue with me.
Blog Credit:
S. Chaudhary
Salesforce Developer
Avenoir Innovations Pvt. Ltd.
Reach us: team@avenoir.ai