What is the right way to query all notes and attachments salesforce?

3.7K    Asked by ClareMatthews in Salesforce , Asked on Feb 24, 2023

I would like to query all notes & attachments, let's 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 kinds of "documents" like files, notes, attachments etc. from my point of view

Answered by Darsh K

There are two sorts of notes and attachments salesforce. There's the Classic type, represented by the Note and Attachments Objects, 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 notes = [SELECT Id FROM Note WHERE ParentId = :myRecord];
List 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 summarise 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 cdls = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :myRecord];
Set documentIds = new Set();
for (ContentDocumentLink cdl : cdls) {
    documentIds.add(cdl.ContentDocumentId);
}
// ContentDocumentLink doesn't support semi-joins, which is frustrating.
List documents = [SELECT Id FROM ContentDocument WHERE Id IN ocumentIds];

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.



Your Answer

Answer (1)

To query all Notes and Attachments in Salesforce, you typically need to query both the Note and Attachment objects separately. Salesforce provides separate objects for Notes and Attachments, and they are queried independently. Here's an example of how you can query both Notes and Attachments in Salesforce using SOQL (Salesforce Object Query Language):

SELECT Id, Title, Body
FROM Note
UNION ALL
SELECT Id, Name, Body
FROM Attachment

In this example:

The first query selects fields (Id, Title, Body) from the Note object.

The second query selects fields (Id, Name, Body) from the Attachment object.

UNION ALL is used to combine the results of both queries into a single result set.

Keep in mind that the fields you select may vary based on your requirements, and you can include additional fields as needed.

Additionally, if you want to filter the results based on certain criteria, you can add WHERE clauses to each query individually. For example:

SELECT Id, Title, Body
FROM Note
WHERE CreatedDate >= TODAY
UNION ALL
SELECT Id, Name, Body
FROM Attachment
WHERE CreatedDate >= TODAY

This query retrieves Notes and Attachments created today.

Remember to adjust the queries based on your specific requirements and the structure of your Salesforce organization.

5 Months

Interviews

Parent Categories