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
Hitesh Khatri 13Hitesh Khatri 13 

How to run the inner queries in Salesforce?

How can I run the query in SOQL?

SELECT ID, ParentId FROM (SELECT ID, ParentID, MAX(LastModifiedDate) maxModified
FROM Attachment 
WHERE ParentID IN ('xxxxxxxxxxxx','xxxxxxxxxxx') AND ContentType IN ('image/png', 'image/jpeg', 'image/gif') GROUP BY Id, ParentID ORDER BY MAX(LastModifiedDate) DESC) t GROUP BY t.ParentID
Best Answer chosen by Hitesh Khatri 13
Hitesh Khatri 13Hitesh Khatri 13

Hi Hemant, 

Thanks for Answering to my query and Sorry for not updating about the iabout the problem but I have already resolved the issue. In my case I used 

 

SELECT Id, (SELECT Id FROM Attachments ORDER BY LastModifiedDate DESC LIMIT 1) FROM custom_object_c


Though your Answer are very helpful. 

 

Thanks You

Regards 

Hitesh Khatri

All Answers

sfdcMonkey.comsfdcMonkey.com
HI Hitesh,
First of all, above query syntax is totally wrong. here is the right way for inner query (child queries in salesforce),
Syntax :
select fields,(select fields from sObjectChildRelationshipName) from sObject

Sample Example 1 :
SELECT Id, Name, Industry, AnnualRevenue,
 ( SELECT Name, Email, BirthDate FROM Contacts )
FROM Account

Sample Example 2 :
SELECT Id, CaseNumber,Status,
 (SELECT Id, Name FROM Attachments)
FROM Case

for full reference :
http://www.sfdc99.com/2013/06/24/example-how-to-write-a-cross-object-soql-query-part-2/

i hope it helps you.
Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
thanks
sfdcmonkey.com
Hitesh Khatri 13Hitesh Khatri 13

Hi Piyush,

 

Thank s for the response. Above queries fetched data from two different table. but what I want is to fetch data with the same table.

In detail:- I want to fetch a list of Attcahment with ID and parentId and I want that the attachment list only contains items with one parentID i.e. only attachment with one parentID which was the last modifield.

For example

List Items;

 

1. PatrentID A, AttachmentID 1 (LastModified) .

2. ParentId B, AttachmentID 1 (LastModified).

 

Not like

1. ParentID A, AttachmentID 1.

2. ParentID A, AttachmentID 2.

 

Can it be possible with a single querry?

Please help.

 

Thanks

Regards

Hitesh Khatri

Hemant_SoniHemant_Soni
Hi Hitesh,
I think you should try like below.
SELECT Id, CaseNumber,Status,(SELECT Id, Name,ParentId FROM Attachments ORDER BY LastModifiedDate DESC)FROM Case
and you can add more condition in inner query like if you have set of parentId then you Can Use that set in inner query as well.
SELECT Id, CaseNumber,Status,(SELECT Id, Name,ParentId FROM Attachments where parentId IN: NameOfSet ORDER BY LastModifiedDate DESC)FROM Case
Thanks
Hemant

 
Hitesh Khatri 13Hitesh Khatri 13

Hi Hemant, 

Thanks for Answering to my query and Sorry for not updating about the iabout the problem but I have already resolved the issue. In my case I used 

 

SELECT Id, (SELECT Id FROM Attachments ORDER BY LastModifiedDate DESC LIMIT 1) FROM custom_object_c


Though your Answer are very helpful. 

 

Thanks You

Regards 

Hitesh Khatri

This was selected as the best answer
Hemant_SoniHemant_Soni
Hi Hitesh,
Please mark your own answer as solved so if any one have same problem then can easily find solution.
Thanks
Hemant