3

I have a Object called SiteContracts__c and it has a Related List Object is Engineer_CheckList__c", now I want to get the Engineer_CheckList__c record which is related to SiteContracts__c how can I retrieve those records using SOQL ?

For the records in Engineer_CheckList__c object we have Lookup to select the SiteContracts__c.

Update:

I am getting the following Error while using this Query through Developer Console

Id, Name, (Select Id, Name From Engineer_Checklist__r) From SiteContracts__c ^ ERROR at Row:1:Column:40 Didn't understand relationship 'Engineer_Checklist__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.

4 Answers 4

3

You can use an inner Query.

SELECT id,name, (SELECT id FROM Engineer_CheckLists__r ) FROM SiteContracts__c

then you can iterate through the list.

for(SiteContracts__c s :[select id,name,(select id from Engineer_CheckLists__r ) from SiteContracts__c] ){
    for(Engineer_CheckList__c e: s.Engineer_CheckLists__r){
          //do something
     }
}

Hope it works for you.

1
  • 1
    Welcome to salesforce.stackexchange Raviteja, using the binary button in the editor your can format code. I've gone ahead and done that for you on this occasion. Oct 10, 2014 at 13:51
3

Karti, probably Didn't understand relationship 'Engineer_Checklist__r says that relationship name is different than 'Engineer_Checklist__r'.

Ensure that relation name is correct, you can use https://workbench.developerforce.com tooll

Go to: Info -> standard & custom obj (choose your object) -> Child relationship -> relationshipName

2

The following SOQL statement will give you a list of SiteContracts__c records with reference to each Engineer_checklist__c record it is related to.

list<SiteContracts__c> cons = [Select Id, Name, (Select Id, Name From Engineer_CheckLists__r) 
                                From SiteContracts__c];

This assumes the child relationship name for your lookup field on the SiteContracts__c object is Engineer_CheckLists. You can confirm this by going to your lookup field and looking for the 'Child Relationship Name' field. whatever is in that field you need to add "__r" to it.

You can add additional fields to each object if you would like. You can learn more about nested queires and relationship queries here

http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/sforce_api_calls_soql_relationships.htm

9
  • Thanks for the Quick help sfdc_ninja but I have already tried this in Developer Console the Query is Parsing an Error
    – Karti K
    Oct 10, 2014 at 13:46
  • What is the error? Oct 10, 2014 at 13:46
  • Id, Name, (Select Id, Name From Engineer_Checklist__r) From SiteContracts__c ^ ERROR at Row:1:Column:40 Didn't understand relationship 'Engineer_Checklist__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.
    – Karti K
    Oct 10, 2014 at 13:51
  • 1
    You did not add the 's' to the relationship name. Try Engineer_Checklists__r Oct 10, 2014 at 13:55
  • 1
    This is a quote from your original question stating there is a lookup. If that is not the case, you should change your question. "For the records in Engineer_CheckList__c object we have Lookup to select the SiteContracts__c." Oct 10, 2014 at 14:09
0

ADD 'S' to the object name in the query,

Select Id, Name, (Select Id, Name From Engineer_CheckLists__r) From SiteContracts__c

Thanks, Raviteja

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .