0

I'm trying to programmatically upload a file (AttachedContentDocument) to an Account as an attachment using the REST API. This doesn't seem to be documented by Salesforce.

When I use the Lightning UI to upload the file on the "Documents" tab of an account, the URL it opens is /lightning/r/AttachedContentDocument/id... so I'm trying to upload a AttachedContentDocument.

I'm crafting a request body that looks like:

--boundary_string
Content-Disposition: form-data; name="entity_attachedcontentdocument";
Content-Type: application/json

{
   "Title": "hello",
   "FileExtension": "png"
}

--boundary_string
Content-Type: application/png
Content-Disposition: form-data; name="Body"; filename="myfile.png"

... binary data of file ....

All of the examples online for inserting files into salesforce use this multipart body. Note I changed the name to name="entity_attachedcontentdocument" to reflect the sObject type being inserted. Then I craft a request:

curl https://mydomain.salesforce.com/services/data/v53.0/sobjects/AttachedContentDocument/ -H 'Authorization: ...' -H "Content-Type: multipart/form-data; boundary="boundary_string"" --data-binary @binaryjson

This errors with:

[{"message":"Invalid field: 'Body'","errorCode":"INVALID_FIELD"}]

Looking at the AttachedContentDocument documentation page there's no documentation on which field data is supposed to go into.

I get the same error changing the endpoint and entity name to /ContentDocument/. And no field name seems to work, not Body/Content/File.

Is it possible to use this API?

1 Answer 1

1

As the documentation you linked states:

This read-only object contains all ContentDocument objects associated with an object.

For more information, see AttachedContentDocument in the Object Reference for Salesforce and Lightning Platform.

If you go there, it says:

Use this object to list all ContentDocument objects attached to an object via a feed post.

To retrieve ContentDocument objects, issue a describe call on an object, which returns a query result for each activity since the record was created. You can’t directly query this object.

So, you actually need a FeedItem, which has superseded the old FeedPost object. The fields you need will be found in the documentation for FeedItem.

2
  • I'm confused. I'm trying to upload a file and attach it to a user account. I don't think i want a FeedItem? I may have found a way to do this with ContentVersion and i'm asking a new question
    – Andy Ray
    Commented Oct 21, 2021 at 17:22
  • @AndyRay Oh, your question just said "Account", so I thought you meant the CRM object. Still, according to the docs, you can attach files to a User this way.
    – sfdcfox
    Commented Oct 21, 2021 at 17:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .