0

There are 3 objects Books__c, Reader__c, Borrowed_by__c.

There is a master-detail field on Borrowed_by__c with Books__c. There is a lookup relationshp field on Borrowed_by__c with Reader__c

I am trying to retrieve the field of these objects in single query. I am using the below queries.

This query is giving results.

select id, name, reader__r.name, from Borrowed_by__c  

These below query are not giving results.

select id, name, reader__r.name, books__r.name from Borrowed_by__c

select id, books__r.name from Borrowed_by__c

select id, (select id, name from books__r) from Borrowed_by__c

The child relationship names are correct. What should I modify in my query to get the fields of both Books__c and Borrowed_by__c or peferably 3 related objects ?

1
  • Please use only relevant tags. This question has nothing to do with lightning-aura-components or dynamic-soql or apex.
    – David Reed
    Dec 5 '20 at 16:19
0

As described, Borrowed_by__c has no children and cannot perform a subquery. It is essentially a junction object, although one of its relationships is a lookup rather than a master-detail for reasons that are not clear to me.

This syntax is correct for traversing a relationship from a child, Borrowed_by__c, to a parent, with the relationship field reader__c:

select id, name, reader__r.name, from Borrowed_by__c  

You would use exactly the same syntax to access Books__c details, using the name of the appropriate relationship field with __r in place of reader__r.

This query

select id, name, reader__r.name, books__r.name from Borrowed_by__c

appears to be correct save that presumably the relationship field name books__r is incorrect.

0

Considering Borrowed_By__c is the junction object, I believe your query is using the child relationship name format As you said this does't work

select id, name, reader__r.name, books__r.name from Borrowed_by__c

Try this (removed s at from books__r):

select id, name, reader__r.name, book__r.name from Borrowed_by__c

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.