Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

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

Asked by Clare Matthews Feb 24, 2023 7.4K views 2 answers
Share

About this question

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

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Apr 26, 2024

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.

Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.