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
ADGADG 

SOQL subquery working for standard objects and not for custom objects

I have a SOQL query which works fine:
SELECT Id, Name, (SELECT Id,Name FROM Contacts)
                    FROM Account
                    WHERE Name = 'Apollo'

But the following queries, which have been similarly formed as the above, do not run:
SELECT Id, Candidate_Name__c, (SELECT Id,Assessment__c FROM Review__c)
                         FROM Job_Application__c
                         WHERE Candidate_Name__c = 'Jamie Sandras'
Error: "Didn't understand relationship 'Review__c' in FROM part of query call...."
 
SELECT Id, Candidate_Name__c, (SELECT Id,Assessment__c FROM Review__r)
                         FROM Job_Application__c
                         WHERE Candidate_Name__c = 'Jamie Sandras'
Error: "Didn't understand relationship 'Review__r' in FROM part of query call...."

where Review__c is child object of Job_Application__c


So I have 2 questions:

1. Is it a limitation that such queries (with sub-queries to child object) work only for standard objects and not for custom objects?

2. In the first query, why does the sub-query uses the object name "Contacts" and not "Contact" which is the actual object name?
SELECT Id, Name, (SELECT Id,Name FROM Contacts) FROM Account ....
Best Answer chosen by ADG
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- Please click on lookup field and then check your child relationship name
Please follow below step :-
1) Click on Review object
2) Then search Job_Application lookup
User-added image

Your query should like below
SELECT Id, Candidate_Name__c, (SELECT Id,Assessment__c FROM Reviews__r)  FROMJob_Application__c WHERE Candidate_Name__c = 'Jamie Sandras'


 

All Answers

RAM AnisettiRAM Anisetti
add 's'
 
SELECT Id, Candidate_Name__c, (SELECT Id,Assessment__c FROM Reviews__r)  FROMJob_Application__c WHERE Candidate_Name__c = 'Jamie Sandras'


 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. i hope that will help you

Understanding Foreign Key and Parent-Child Relationship SOQL Queries
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SOQL_foreign_key.htm

Basic SOQL Relationship Queries
2) https://developer.salesforce.com/blogs/developer-relations/2013/05/basic-soql-relationship-queries.html

A Deeper look at SOQL and Relationship Queries on Force.com
3) https://developer.salesforce.com/page/A_Deeper_look_at_SOQL_and_Relationship_Queries_on_Force.com

Using Relationship Queries
4) https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_query_using.htm


Lets Elaborate the Example Using Account and Contact.

--> Parent Object : Account
--> Child Object : Contact
--> Child Relationship Name : This is very important and is required for parent to child queries. You can find the same in the relationship field that relates the child object to account. In our case it is "Account" which is located @Contact. So if you go to detail of this field you will find the Child Relationship Name, which should be "Contacts". So the child to parent relationship will have this in its from statement. If it was a custom object you will have to add "__r"

NOTE:- Please click on lookup field and then check your child relationship name

 
Arunkumar RArunkumar R
Hi,

Try the below one,

As per your query,

Parent Object : Job_Application__c
Child Object: Review__c

Step 1: Go to child object review.
Step 2: Find the relationship field(lookup/master) to the job application object.
Step 3: Click on the relationship field.
Step 4: On the detail of relationship field you will find the "​Child Relationship Name".
Step 5: Copy the Child relationship name.
Step 6: Build query as follow,


SELECT Id, Candidate_Name__c, (SELECT Id,Assessment__c FROM Review__r) FROM Job_Application__c WHERE Candidate_Name__c = 'Jamie Sandras'

 
Amit Chaudhary 8Amit Chaudhary 8
NOTE:- Please click on lookup field and then check your child relationship name
Please follow below step :-
1) Click on Review object
2) Then search Job_Application lookup
User-added image

Your query should like below
SELECT Id, Candidate_Name__c, (SELECT Id,Assessment__c FROM Reviews__r)  FROMJob_Application__c WHERE Candidate_Name__c = 'Jamie Sandras'


 
This was selected as the best answer
ADGADG
Thanks for your responses. I went the the responses and my confusions got cleared now.
I am now able to work it out fine.
Pankaj  Kumar MauryaPankaj Kumar Maurya
parent object:school
child object student

Here,i am doing count all the student who are associated with particular school object.
I am using subquery which is working perfectly,If you find this suitable Plz like the Content

Code:
 void validateCount(list<student__c> TN)
    {
     list<school__c> lstSch=new list<school__c>();
        set<id> idset=new set<id>();
        for(student__c objST:TN)
        {
            if(objSt.School__c!=null)
            {
            idSet.add(objST.School__c);
            }
        }
        if(idSet.size()>0)
        {
            for(school__c objSch:[select id,number_of_student__c,(select id from Students__r) from school__c where id in:idset])
            {
              objSch.number_of_student__c=ObjSch.Students__r.size();
                lstSch.add(objSch);
            }
            if(lstSch.size()>0)
            {
                update lstSch;
            }
        }
        
    }