I'm new to Salesforce, I have a form and at the bottom there is a section to upload files that are linked to that form. I have been reading a lot ob how to upload that document using the REST API and after days of reading I can't figure out.

This is the bottom part of the form:

enter image description here

I think this is the json to upload the file to the custom object:

Content-Disposition: form-data; name="entity_document";
Content-Type: application/json
    "Description" : "Marketing brochure for Q1 2011",
    "Keywords" : "marketing,sales,update",
    "FolderId" : "005D0000001GiU7",
    "Name" : "Marketing Brochure Q1",
    "Type" : "pdf"
Content-Type: application/pdf
Content-Disposition: form-data; name="Body"; filename="2011Q1MktgBrochure.pdf"
Binary data goes here.

Im using this function:

 Public Sub attachNewDocument(ByVal FileName As String, ByVal fileExtension As String, ByVal fileDescription As String, ByVal fileKeywords As String, ByVal documentID As String, ByVal fileBinary As String)

    Dim StringBinary As String = ConvertFileToBase64(fileBinary)

    Dim req As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(strInstanceUrl + API_ENDPOINT + "sobjects/Document/")
    Dim contentDisposition As String = String.Format("Content-Disposition:form-data;name={0}entity_document{0}",Chr(34))
    req.ContentType = "application/json"
    req.Method = "POST"
    req.Headers.Add("Authorization: Bearer " & strAuthToken)
    req.Headers.Add("X-PrettyPrint", "1")
    'Dim postDocAsString As String = JsonConvert.SerializeObject(jsonFields)
    Dim bytes As Byte() = Encoding.UTF8.GetBytes(jsonFields)
    req.ContentLength = bytes.Length
    Dim os As System.IO.Stream = req.GetRequestStream()
    os.Write(bytes, 0, bytes.Length)
    Dim resp As System.Net.WebResponse = req.GetResponse()
End Sub
  • What specifically have you tried? Can you edit your post to provide an example of your current strategy?
    – David Reed
    Aug 27 '20 at 18:56
  • @DavidReed Can you point me to the steps to accomplish this task?, or an online example that can point me to the right direction?
    – RicEspn
    Aug 27 '20 at 20:19

There are three sObjects that can represent a "file". It's likely you are uploading to the wrong one, Document.

Those are Attachment, ContentDocument, and Document. The sObjects that are used for files attached to records are Attachment (old-style) and ContentDocument (Lightning).

The example you show appears to be a custom UI, so it's not immediately clear which object is being used. You may need to run some SOQL queries to find out. (It could be as simple as doing a count() query against each object, adding a file, and then re-running the queries to see what changed).

It's most likely that you will need to upload a ContentDocument.


Depending on how you want to relate the file to your record (and potentially if you have Chatter active in your org), you may be able to use the Connect and/or Chatter REST APIs to upload a file to the appropriate place, for example you can upload to Files Home using the following endpoint: /services/data/v{{version}}/connect/files/users/me https://developer.salesforce.com/docs/atlas.en-us.chatterapi.meta/chatterapi/connect_resources_users_files_general.htm

After saving the Id of the file from the response, you can create a feed item with a file attached or include it as an inline with either of the following endpoints: /services/data/v{{version}}/chatter/feed-elements/ /services/data/v{{version}}/chatter/feed-elements/batch https://developer.salesforce.com/docs/atlas.en-us.chatterapi.meta/chatterapi/features_feeds_feed_elements.htm OR create a file share for a record with the following endpoint: /services/data/v{{version}}//connect/files/fileId/file-shares https://developer.salesforce.com/docs/atlas.en-us.chatterapi.meta/chatterapi/connect_resources_files_shares.htm

I currently have an open PR on the Salesforce APIs for Postman GitHub project that should assist with these requests, but I also suggest using Postman and this project to help build the callouts anyway: https://github.com/scolladon/postman-salesforce-apis

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.