1

I am unable to understand relationships with DML/SOQL.

Example, I have custom objects Course__c and Course_line_item__c. Course_line_item__c has a lookup field on it that points to Course__c. So this means multiple Course_line_item__c's can have the same parent Course__c, correct?

So I have this SOQL Query :

list<Course__c> LC=[SELECT Id, (SELECT Id FROM Course_line_item__c) FROM Course__c];

and I'm getting this lovely error :

Compile Error: Didn't understand relationship 'Course_line_item__c' 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. at line 13 column 12

What am I doing wrong???

EDIT : me custom relationship field is called CourseLU__c.

  • Use the Child Relationship Name instead of the child object api name. – Krishna Bidwai Jul 22 '16 at 11:27
  • eg, [SELECT Id, (SELECT Id FROM CourseLU__c) FROM Course__c]? CourseLU__c is the name of the custom lookup relationship field on the line item to Course__c. Crossing my fingers. EDIT : nope, I donot understand how to do this. – fraglord Jul 22 '16 at 11:33
3

As the error message saying, the relationship name you are using is incorrect. __r is used for custom relationships in salesforce(Course_line_item__r); and plural form of the object when refer to child object. It should be something like below.

list<Course__c> LC=[SELECT Id, (SELECT Id FROM Course_line_items__r) FROM Course__c];

Make sure you get the correct relationship name. You can refer to your WSDL for this if you are not sure.

  • WTF why does your statement work??? this is one of my previous attempts : list<Course__c> LC=[SELECT Count__c,Fees__c,Id,Name,User__c, (SELECT Attended__c,Course__c,Name,Name__c FROM Course_line_item__r WHERE Course_line_item__r.Course__c.ID=:cid) FROM Course__c WHERE ID=:cid];. How come YOURS worked and mine didnt? EDIT: barebones version : SELECT Id, (SELECT Id FROM Course_line_item__r WHERE Course_line_item__r.Course__c.ID = :cid) FROM Course__c WHERE ID =: cid – fraglord Jul 22 '16 at 11:40
  • 1
    If it didn't work, that means that we have some mistake. Compilers, interpreters doesn't have favors for anyone, they works as they are developed. I think you had missed the plural format(s at the end) in your sub-query. – highfive Jul 22 '16 at 11:45
1

Just going to share how to find the child relationship name yourself via Execute Anonymous:

for (ChildRelationship relationship : SObjectType.Course__c.getChildRelationships())
    if (relationship.getChildSobject() == Course_Line_Item__c.sObjectType)
         system.debug(relationship.getRelationshipName());

The above will yield the verbatim name you should use in your subquery, which would then look like:

(SELECT Id FROM Course_Line_Items__r)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.