1

I would like to query all notes & attachments, lets say for an Account. I uploaded 2 different files in the notes and attachment area but if I do the query:

select id from attachment

I do not get anything back. If I make a query against Notes:

select id from Note

same result. I get one result back but this is a different Note The query:

SELECT Id, ParentId from ContentDocument 

gives me the correct values but the ParentId is empty. Therefore, I do not know which Documents are for which accounts.

Any ideas how to query it in the right way? Currently, SF has too many different kind of "documents" like files, notes, attachments etc. from my point of view.

3

Salesforce has two sorts of notes and attachments. There's the Classic type, represented by the Note and Attachment sObjects, and the Lightning type, represented by the Content suite of objects - ContentDocument, ContentVersion, ContentDocumentLink, and ContentNote. These two types are accessed in quite different ways.

The classic Note and Attachment records have a single polymorphic lookup to the record to which they're attached. You can query them simply:

List<Note> notes = [SELECT Id FROM Note WHERE ParentId = :myRecord];
List<Attachment> attachments = [SELECT Id FROM Attachment WHERE ParentId = :myRecord];

In the Lightning world of Content, the object model is quite a bit more complex. Both Notes and Attachments are represented as ContentDocument, a parent object over potentially multiple ContentVersion records. The ContentDocument is then linked to all of the locations in which it's been shared, which may be many, via ContentDocumentLink.

To make things even more confusing, there's a facade object, ContentNote, that acts like a ContentDocument with a single ContentVersion inherent in its Content field. You'll mostly have to worry about ContentNote if you're adding Notes, however.

Anyway - not to try to summarize the substantial complexity involved in the Content system - to query both Notes and Attachments in the Lightning style for some object whose Id is myRecord, you'd do

List<ContentDocumentLink> cdls = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :myRecord];
Set<Id> documentIds = new Set<Id>();
for (ContentDocumentLink cdl : cdls) {
    documentIds.add(cdl.ContentDocumentId);
}
// ContentDocumentLink doesn't support semi-joins, which is frustrating.
List<ContentDocument> documents = [SELECT Id FROM ContentDocument WHERE Id IN :documentIds];

This will return both ContentNote and regular ContentDocument Attachments. If you want just Notes, query on ContentNote rather than ContentDocument. If you want just Attachments and not Notes, filter on FileType != 'SNOTE'.

To get the actual content, for ContentDocument, you can execute a ContentVersion query against ContentDocumentId and IsLatest = true. For ContentNote, you can simply ask for the Content field in your ContentNote query, which is a Blob field.

To delete records, you only need to delete the top-level ContentDocument record. That will take the ContentDocumentLink records with it. Deleting ContentDocumentLink records may make an Attachment inaccessible, but it won't be deleted entirely.

  • Thx for this great answer! I had also found out the solution with the ContentDocumentLink and also could delete it but than only - like the name says it - the link in the account was removed but the real document still was in the system. So in order to delete both I need to to make two dml statements right? – utm Feb 1 at 15:21
0

Ok I think I found the solution:

select id, (select parentId from CombinedAttachments) from account where id...

than I get a list with these objects

  • Its still not possible to delete those attachments aaarrrr. Maybe this is still the wrong way. Any suggestions? – utm Feb 1 at 13:43

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.