function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tbfan6789tbfan6789 

Question on SOQL nest queries (Attachments and Accounts)

Hi, I'm hoping someone can assist me with this. I'm trying to work on a search engine in Salesforce that allows the user to look up the Account Information (Name, Account Identifier, etc..) and the Attachment File Name at the same time. However, I was receiving SOQL errors when trying to join the attachment and account objects (due to the Polymorphic relation via ParentID). What is the best way in Apex to have a list of attachments that include the Account information on each element/record?

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

You can do this:

 

[select id,name,accountnumber,(select id,name from attachments) from account limit 1]

Note that you still need a second query to retrieve the Body field, if you want it; binary fields can't be part of a subquery. However, if all you need is a list of attachments, this will work.

All Answers

sfdcfoxsfdcfox

You can do this:

 

[select id,name,accountnumber,(select id,name from attachments) from account limit 1]

Note that you still need a second query to retrieve the Body field, if you want it; binary fields can't be part of a subquery. However, if all you need is a list of attachments, this will work.

This was selected as the best answer
tbfan6789tbfan6789

Thanks for the information. That should help.

Kris AnglinKris Anglin
tbfan6789 I was looking for the same search function.  Were you able to create this?