I am new to file handling in Salesforce. I want to grab all the files attached to all the records of a custom object. Can anyone help me with it?
3 Answers
Old-school SF used Attachment
object and you could simply go SELECT Body, ContentType, Name FROM Attachment WHERE ParentId = '...'
.
In Lightning there's high chance your attachments are called "Files" (the actual API name is ContentDocument
/ ContentVersion
). Check if the attachment's ID starts with 068 or 069. They aren't linked directly to your record. Instead there's ContentDocumentLink
table sitting in between, used to cross-share same file. (You upload it and waste database space once, then you can cross-link it in Chatter posts, groups, other records...)
The official ERD isn't great, try to click through it in Setup -> Schema Builder or this answer might help: https://salesforce.stackexchange.com/a/160285/799. There's a sample query which you might have to fine-tune a bit, for example to SELECT ContentDocument.LatestPublishedVersion.VersionData
to get the actual payload.
Check out other questions around here about ContentVersion
. For example https://stackoverflow.com/a/48668673/313628 (it's other way around, about upload but should give you good idea).

- 18,088
- 2
- 34
- 46
-
P.S. If you have Email-to-Case there's chance there are Files that appear on the Case related list but their `ContentDocumentLink` links them to `EmailMessage`, not to `Case`. There's setup option that enables that. So maybe you need 2 queries, get Case and related Email ids, then all CDLs -> CDs -> latest file versions. – eyescream Sep 03 '20 at 13:33
AppExchange tool you can try for bulk download, Satrang Mass File Download AppExchange App helps to download Bulk of Files and Attachments from Standard and Custom Objects just within a few clicks.
URL - https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000EcsAOUAZ&tab=e
Disclaimer: I work at Satrang Technologies, the publisher of this Mass File Download AppExchange App.

- 21
- 1
You can export all notes and attachments using open-source sfdx-hardis plugin command
- First, define a files export
sfdx hardis:org:configure:files
When prompted for SOQL request, you can input SELECT Id,Name FROM YourCustomObjectName__c
- Then, run the files export
sfdx hardis:org:files:export
More details in this article
Disclaimer: My company authors sfdx-hardis plugin

- 564
- 7
- 14