+ Start a Discussion
SFDC AVINASHSFDC AVINASH 

SOQL on related objects.

Hi all,
I am having a problem with bellow SOQL.
SELECT Name, (SELECT Name FROM Resource__r) FROM Projects__c

My Resource__c object has a lookup field for Projects__c. When i try to execute the Query it will throw me an error informing

"ERROR at Row:1:Column:36
Didn't understand relationship 'Resource__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names."

Please help me to resolve this. Also any material to understand the Related objects SOQL will be greately helpful for me to enhance my knowledge.

Thanks in advance. 
BalajiRanganathanBalajiRanganathan
Try this one.
SELECT Name, (SELECT Name FROM Resources__r) FROM Projects__c

basically you have to 
1) Go to the relation ship field on the child object(Resource__c )
2) Click to View. and look at the attribute Child Relationship Name

you have to use (SELECT Name FROM <Child Relationship Name>__r in your Parent to Child Relation Ship query

The link below will help

http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/sforce_api_calls_soql_relationships.htm
SFDC AVINASHSFDC AVINASH
Thank you so much Balaji for sharing the URL... I was tired of searching for the good resources on this topic. Your query worked.. But i am wondering how! When i check the attribute of Child Obj (Resource) its showing Resource__c (API Name). Whereas for Projects its similar as we used in our SOQL (Projects__c). Please find the attached screenshot which might give better picture of what i am explaining. Regards, Avinash
BalajiRanganathanBalajiRanganathan
You have to go to the child relationship field (Might be Projects__c) on the child object not the child object itself.
If you view the relationship field, it will have one of the attribute as Child Relationship Name