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--
4 Comments
[…] https://pupilstuff.wordpress.com/2020/06/07/attach-files-to-salesforce-record-using-just-one-rest-ap… […]
hi, did you find to do that with just one api call?
Hi Lin,
I am not able to upload a file to content version using salesforce standard rest api.. plz help
i am performing this scenario using work bench. I have a file in my desktop and to upload that file into salesforce.. below are the JSON request body.
{
“Title” : “Test”,
“PathOnClient”:”C:\Users\Desktop”,
“ContentLocation”:”S”,
“VersionData”:”Test contentversion using Standard API”
}
Error: the request resource doesnt exists.
I got it. please ignore my previous comment.