I have three custom objects: Book, Loan, and Reader. Loan is a junction object with lookup relationship with both Book and Reader. The diagram below shows their relationship:

enter image description here

I want to write a SOQL query to retrieve the Loan name and Active status from Loan object, Book title and subject from Book object, and Reader name and specialization subject from Reader object.

I wrote the following query but it showing an error:

SELECT Active__c, Name, 
       (SELECT Name, Subject__c FROM Book__r), 
       (SELECT Name, Specialization_Subject__c FROM Reader__r) 
FROM Loan__c

I get the following error message:

Didn't understand relationship 'Book__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.


You've written your query in parent-child subquery form, but Loan__c is in fact the child object. You need to traverse the relationship upwards to obtain data from the related Book__c and Reader__c objects:

SELECT Active__c, Name, 
       Book__r.Name, Book__r.Subject__c, 
       Reader__r.Name, Reader__r.Specialization_Subject__c 
FROM Loan__c
  • that works fine. thank you
    – Hari Ha
    Jul 10 at 21:12

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.