19

Previously, on Salesforce Classic, when a file was uploaded, it was saved on the Attachment object table. This object had the Body field.

On Lightning Experience, however, attachments are stored as Content Documents, a completely different (and arguably more complicated) set of tables.

How can I retrieve the file's body when using Lightning Experience? I mean, through SOQL.

1 Answer 1

34

The Content tables are considerably more complex than Attachments. The file content is held in the ContentVersion object; one ContentDocument has one or many versions, as well as an arbitrary number of ContentDocumentLink records connecting it to records to which it is shared.

Supposing that you already have the Id of a ContentDocument attached to some record or another, you can get the relevant file content in SOQL:

SELECT VersionData FROM ContentVersion WHERE ContentDocumentId = :myId AND IsLatest = true

The ContentVersion object reference may be useful.

If you need to acquire files that are attached to a specific object in Salesforce, you'd need to first perform a query against ContentDocumentLink and filter upon LinkedEntityId with the Id of the record-attached-to. The ContentDocumentLink field ContentDocumentId will then let you perform the above query against ContentVersion.

5
  • 1
    Can you serve it via API as well ?
    – Edmondo
    Apr 26, 2020 at 16:24
  • Hi David, Is the process same for an HTTPGet API (Custom one) to pass on the document to mulesoft ? Jun 11, 2020 at 3:34
  • @user2598808 For new questions, please open a new post.
    – David Reed
    Jun 11, 2020 at 4:55
  • I am confused on how you retrieve the actual bod, lets say that this new file is a json file and you want to read its content?
    – manza
    Sep 27, 2020 at 23:45
  • @monza It's the VersionData blob field shown above. If it contains a string, you call its toString() method.
    – David Reed
    Sep 28, 2020 at 0:35

You must log in to answer this question.

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