0

A custom object B has a custom field, which is a Lookup on custom object A.

So this means that for each B there can be only ONE A, yes? And multiple B's can have same A, or a single A can have multiple B's related to it, correct?

Basic SOQL relationship queries

From above, I'm doing :

SELECT A__c.Id, A__c.Name, (SELECT B__c.Id, B__c.Name FROM B__c) FROM A__c

It is NOT working.

And ofc the code below wont even work, right?

SELECT Id, Name, A__c.Id, A__c.Name FROM B__c

What am I doing wrong here? I am completely out of my depth here. Complex SOQL relationships hurt my brain just thinking about them.

EDIT :

The field on object B__c reads :

Related object - Related_object__c - Lookup(object)

I tried

SELECT A__r.Id, A__r.Name, (SELECT B__r.Name FROM B__c) FROM A__c

but yet again, its not working.

5
  • It's __r, not __c. There's tons of questions about this already. See if that helps you?
    – sfdcfox
    Commented Aug 17, 2016 at 7:58
  • I tried SELECT A__r.Id, A__r.Name, (SELECT B__r.Name FROM B__c) FROM A__c but yet again, its not working. 'Related object Related_object__c Lookup(object)` I replaced the actual name with "object". But I do hope you understood how this is structured.
    – fraglord
    Commented Aug 17, 2016 at 8:04
  • 1
    SELECT Id, Name, (SELECT Id, Name FROM B__r) FROM A__c sounds more like what you're looking for?
    – sfdcfox
    Commented Aug 17, 2016 at 8:06
  • 1
    You need to use plural relationship names. In your case you need to use Bs__r. Please see the answer below Commented Aug 17, 2016 at 8:08
  • Worked, sorta. SELECT Id, Name, (SELECT Name FROM Bs__r) FROM A__c. Mind making it an answer, SFDCFox? Also, WHY is SoQL so confusing!?!?!?!!!
    – fraglord
    Commented Aug 17, 2016 at 8:42

2 Answers 2

3

You need to use the relationship name when using parent-child soql queries.

SELECT A__c.Id, A__c.Name, (SELECT B__c.Id, B__c.Name FROM Bs__r) FROM A__c

please refer this link to understand the relationships in a better way.

Understanding Relationship Names

3
  • Worked, sorta. SELECT Id, Name, (SELECT Name FROM Bs__r) FROM A__c.
    – fraglord
    Commented Aug 17, 2016 at 8:43
  • A problem : common.apex.runtime.impl.ExecutionException: SObject row was retrieved via SOQL without querying the requested field: B__c.Related_object__r . I tried the query in force.com explorer AND workbench and it DOES return the related object name + object...
    – fraglord
    Commented Aug 17, 2016 at 9:01
  • This error comes when we use a field without including it into SOQL. Check if there is any and then add them into your SOQL. Commented Aug 17, 2016 at 9:24
1
  • Parent To Child:
  • select id, (select id from contacts) from account - For standard relationship
  • select id, (select id from Child_Relationship_Name__r) from customobject__c - For custom relationship
  • Child To Parent
  • select id, Account.name from contact - For standard relationship
  • select id, customobject__r.name from customobject__c - For custom relationship

You must log in to answer this question.

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