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
Sailor67Sailor67 

How to handle SOQL parent-to-child query in 2-levels?

I am trying to query Account-Opportunity-OpportunityLineItem using the query below. It does not work and I am aware the documentation says SOQL queries only manages 1-level of parent-to-child relationship...

 

However, is there any trick I can use to rewrite the query without the need of running two separated queries?

 

SELECT Account.Name,

 (SELECT Opportunity.Name, (SELECT OpportunityLineItem.Description FROM OpportunityLineItems)

FROM Opportunities) FROM Account

Best Answer chosen by Admin (Salesforce Developers) 
Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Instead traverse from child to parent

All Answers

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

Instead traverse from child to parent

This was selected as the best answer
Sailor67Sailor67

Yes that would be a possibility, but it would not result in the same hierarchical result.

 

I had hoped to be able to create a result where you go from each account and then down to a list of Opportunities and OpportunityLineItems under each respective account. Going from child to parent will give a result the other way around, if I am not mistaken.

 

But thanks anyway