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
Pooja AroraPooja Arora 

SOQL query on Parent child and child Parent relationship

Hi, I am new to SOQL and still learning.
My use case is: Program has a lookup to Project. 
Project has a checkbox field called "Ready".  When this field is checked, the query should bring the program Id.

This is what I have so far:
Select Name, (select Id from Program__r) from Project__c where Project__r.Ready = true 

It's throwing an error "didn't understand relationship  'Program__r' in From part of query call

Thank you.  
Maharajan CMaharajan C
Hi Pooja,

Try the below changes: 

Please Check your Child Relationship Name from your Project Lookup Field under lookup Options(Open the Lookup Field there you can find this Info ) in Program Object.

Select Name, (select Id from Programs__r) from Project__c where Ready__c = true   // This is Inner query from Parent Object

or try the below one:

Select id from Program__c where Project__r.Ready = true    // Directly Querying the Program Records

Thanks,
Maharajan.C
Ajay K DubediAjay K Dubedi
Hi Pooja,
Follow this link for information about the Query in apex:
https://blog.jeffdouglas.com/2010/02/22/soql-how-i-query-with-thee-let-me-count-the-ways/
and for Querying for child records more than 1 level deep and get information about how to query parent and child record follow this link:
https://www.cloudgiants.com/blog-detailed/2016/8/2/apex-development-querying-for-child-records-more-than-1-level-deep-a-json-workaround
And you can fetch the program Id directly by this query:
SELECT Program__c FROM Project__c WHERE Ready__c = True
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi