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
Pun!5h3rPun!5h3r 

Create a parent to child to child relationship

I want to create some query like:
SELECT ID, Name , dc__Contact__r.Account.Name, (SELECT ID, (select id from product2__r) From dc__Transaction_Line_Items__r) From dc__Transaction__c


I have to pass transaction object record as a parameter. How to Break down the query and accomplish it.
Note: I should get Transaction record which has fields of the child object dc__Transaction_Line_Items__r and sub child object Product__r
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi,
SOQL: Parent to Child and Child to sub child relationship please refer the below link for reference. Hope it will be helpful.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar
Pun!5h3rPun!5h3r
Hello Rahul,
Thanks for a quick reply, If you could please elaborate how do we breakdown query from the given link.
 
Salesforce DeveloperSalesforce Developer
Parent to Child:
Only one level of parent-to-child relationship can be specified in a query. For example Case is child of Account hence you can write like: 

SELECT Id, (SELECT Id, CaseNumber FROM Cases) FROM Account WEERE Id = '0019000000000000'
If you go one more level like:

SELECT Id, (SELECT Id, CaseNumber, (SELECT id FROM CaseComments ) FROM Cases) FROM Account WHERE Id = '0019000000000000'

You will get error something like: MALFORMED_QUERY: SOQL statements cannot query aggregate relationships more than 1 level away from the root entity object.

From Child to Parent:
No more than five levels can be specified in a child-to-parent relationship. For example:

SELECT Id, Parent.CaseNumber, Parent.Account.Name, Parent.Account.Owner.Name, Parent.Account.Owner.Profile.CreatedBy.Name FROM CaseComment