I am trying to extract notes and attachments related to a custom Salesforce Object. I have tried to use the workbench and data loader. Data loader does not work because you cannot use nested SOQL queries, and the workbench doesn't work because I cannot create a bulk load .csv file with the nested query. I cannot create a report in the UI either, as file and content reports don't seem to include the parent object details.

Basically, is there any way to export all notes and attachments associated with a specific custom object without creating custom apex or downloading a 3rd party app?

Here is my query:

select Id, Name, CreatedDate,
(SELECT Id, Name, CreatedDate FROM Attachments), 
(SELECT Id, Title, CreatedDate FROM Notes)
from CustomObject__c
  • 2
    You should be able to load them as two separate queries directly against the child tables. So try SELECT Id, Name, CreatedDate FROM Attachment.Type = 'CustomObject__c' And similarly for Note. – Adrian Larson Jul 3 at 16:37
  • 1
    But be aware that new-style Notes and Files don't attach like Attachments, so if your org is using Lightning Files anywhere, that's a third, more complex query against ContentDocument/ContentDocumentLink. – David Reed Jul 3 at 17:33
  • Thank your quick response and your help on this! However, it looks like this query isn't working either. Here is what I entered: SELECT Id, Name, CreatedDate FROM Attachment.Type = 'Entitlements__c'. From there, I received a Malformed_Query error. I am trying this in the workbench - should I be using a different tool? Thanks again! – Jen Pirrone Jul 3 at 18:16
  • I think there's just a typo. Try SELECT Id FROM Attachment WHERE Parent.Type = 'Entitlements__c' – David Reed Jul 3 at 18:24
  • Success!! You're a life-saver. Thank you! – Jen Pirrone Jul 3 at 18:24
up vote 1 down vote accepted

I'm going to add a little more detail here.

There's a total of four kinds of note and attachment object you might be seeing on your objects, and they're queried in different ways.

Classic Attachments

SELECT Id FROM Attachment WHERE ParentId = 'SOME_ID'

or

SELECT Id FROM Attachment WHERE Parent.Type = 'SOME_SOBJECT_TYPE'

will get you data on your old-style Attachments.

Classic Notes

These work just like Attachments, but with a different sObject:

SELECT Id FROM Note WHERE ParentId = 'SOME_ID'

or

SELECT Id FROM Note WHERE Parent.Type = 'SOME_SOBJECT_TYPE'

Content Notes and Content Documents

Content Notes are the new-style "Enhanced Notes". They're built on the Content architecture under the hood, and have rich-text bodies.

Content Documents (with associated Content Versions) represent the new Lightning Files.

Unfortunately, both are related to records using the ContentDocumentLink object, rather than having a direct lookup, and this comes with some significant query limitations:

You can't run a query without filters against ContentDocumentLink. You can't filter on ContentDocument fields if you're filtering by ContentDocumentId. You can only filter on ContentDocument fields if you're filtering by LinkedEntityId.

You can't filter on the related object fields. For example, you can't filter on the properties of the account to which a file is linked. You can filter on the properties of the file, such as the title field.

A SOQL query must filter on one of Id, ContentDocumentId, or LinkedEntityId.

So, to be short, you can't query all ContentNote or ContentDocument records attached to some specific type of object easily.

You'd have to do something like exporting all Ids for a given sObject type and iteratively querying sets of those Ids against the ContentDocumentLink table to locate attached files. This is something I've done before (and written about here), but it takes some Python or another scripting language to get it done.

If you do have a specific Id or Ids of records whose attachments you want to find, you can do it like this:

SELECT ContentDocumentId, ContentDocument.Title FROM ContentDocumentLink WHERE LinkedEntityId = 'OBJECT_ID'

Note that Content Notes are returned in a Content Document query - they are just a special type of Document.

It looks like you already found out your answer, but the way you formulated your question makes it look like you want the actual content of those attachments (like pdf files associated with an account, for example), so I'll just leave this answer anyway.

You can export the data in your organisation through the Data Export feature, which can be found on Setup > Data > Data Export. You can choose to schedule that export or choose to export now. On the same page, you can select which objects to include.

export page

So you would check both checkboxes, and select just the "Account" checkbox, for the example I proposed. The downside is that it will export everything on every account in the organisation. Fortunately, the data is organised by the record identifier, so you can easily find the documents associated with a specific account.

  • Thanks! This is also good to know. – Jen Pirrone Jul 10 at 13:53

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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