Attach Files to Salesforce record using just one REST API call

There are three api calls required to attach a file to salesforce record, for me its too many api calls for very simple insert.There is another way to perform same action using with just one api call.Lets check first the conventional way to attach a file.

1. Create ContentVersion

First you create the ContentVersion

boundary_string
Content-Disposition: form-data; name=\"entity_content\";
Content-Type: application/json
{
\"ContentDocumentId\" :\"0691q000000i5Jk\",
\"ReasonForChange\" : \"Marketing materials updated\",
\"PathOnClient\" : \"Bestelling 4501146724.txt\"
}

--boundary_string
Content-Type: text/plain
Content-Disposition: form-data; name=\"VersionData\"; filename=\"Bestelling 4501146724.txt\"

VGVzdGluZw==
--boundary_string--

Response

{
    \"id\" : \"068D00000000pgOIAQ\",
    \"errors\" : [ ],
    \"success\" : true
}
2. Retrieve the ContentDocument\’s Id   

Retrieve or query the new ContentVersion to get the ContentDocument\’s Id for creating ContentDocumentLink records.

3. Create ContentDocumentLink 

 After getting the ContentDocumentID use ContentDocumentLink to attach a file to any record

{
  ContentDocumentId: \'0691q000000i9K3\',
  LinkedEntityId: ‘00Q1q000002ZHc7\', // This is the Salesforce Record Id
  ShareType: \"V\"
}

Using FirstPublishLocationId

when creating a new ContentDocument in this way, you can also create an initial ContentDocumentLink by way of the FirstPublishLocationId field.

URL :https://test.salesforce.com/services/data/v23.0/sobjects/ContentVersion
--boundary_string

Content-Disposition: form-data; name=\"entity_content\";
Content-Type: application/json
{
\"Title\":\"Bestelling 4501146724\",
\"PathOnClient\" : \"Bestelling 4501146724.txt\",
\"FirstPublishLocationId\":\"aIC1j00000001OcGAI\"
}
--boundary_string
Content-Type: text/plain
Content-Disposition: form-data; name=\"VersionData\"; filename=\"Bestelling 4501146724.txt\"

VGVzdGluZw==

--boundary_string--

Use This as Object Reference 

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you also interested in writing a blog on a Salesforce topic?Write it on Salesforcerush and let’s spread the knowledge together. We don’t care if you are a beginner so don’t hesitate.

Edit Template

About

We at Salesforcerush translate Salesforce knowledge into blogs.The idea is to circulate practical salesforce experience across all kinds of audiences. Salesforcerush is a platform for all experts who strive to share their knowledge with others.It would have a variety of topics like certifications, new releases, product features, and industry-specific information.We need your support to grow this platform.

Categories

Contact us

Collaborate With Us