function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
S DS D 

Help with SOQL

Hi, am new to SOQL and trying to learn with workbook : but somehow I have observed none of the SOQL for parent child traversing works for me :I am executing this from Developer console and WorkBench. Both giving the below error:

My SOQL is : 
select id, name, (SELECT Name
FROM Job_Application__r)
from Position__c

Just trying to execute the query from workbook. Is there any setting required to make in tools.??



SOQL error from WOrkbench

Thanks!!
Best Answer chosen by S D
James LoghryJames Loghry

I've seen a couple people struggling with this lately, but the best way I have found to determine the Child Relationship API name (in this case it is Job_Applications__r) is to do the following:
 

  1. Go to Setup->Create->Objects (Or Setup->Customize)
  2. Look at the fields of the *Child* Object in the relationship
  3. Click on the Parent relationship (In this case it would be a M/D or Lookup to Position__c)
  4. Take a look at the "Child Relationship Name"
  5. If it's a managed package field, add namespace__ before it
  6. If it's a custom relationship, then add the __r after it.
In the example below, I would use "Accounts__r".

User-added image

All Answers

Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Hi SD,

You need to enable parent relationship queries in workbench to be able to use relationship queries.
Click on workbench Icon >> Settings >> Allows SOQL Parent Relationship Queries >> Apply Settings.

If this helps, mark this as best answer.

Thanks,
N.J
S DS D
NJ, am executing this with this settings only.

Have even executed this on Developer Console. Still the same.

Virendra ChouhanVirendra Chouhan
Hi S D,

N J is 100% correct.
And also i think you forget to write ' '.
I mean to say that is it right name of child relationship?
i think it should be " Job_Applications__r ".
So try this 
select id, name, (SELECT Name FROM Job_Applications__r) from Position__c

Regards
Virendra

S DS D
Virendra , if you see my Workbench Screenshot , i have created the object as  "Job_Application__c"  only (without  'S') ....  : ).

I have even verified this against the Object Definition API name from application.

ALso I tried to write a similar query on Merchant and LineItem and it is failing as well. so thought of putting this query in here.

But do you see any other particular reason  with the error message pasted?


James LoghryJames Loghry

I've seen a couple people struggling with this lately, but the best way I have found to determine the Child Relationship API name (in this case it is Job_Applications__r) is to do the following:
 

  1. Go to Setup->Create->Objects (Or Setup->Customize)
  2. Look at the fields of the *Child* Object in the relationship
  3. Click on the Parent relationship (In this case it would be a M/D or Lookup to Position__c)
  4. Take a look at the "Child Relationship Name"
  5. If it's a managed package field, add namespace__ before it
  6. If it's a custom relationship, then add the __r after it.
In the example below, I would use "Accounts__r".

User-added image
This was selected as the best answer
S DS D
James, Thanks for the detailed explanation. This solved my issue.

However I want to know on pt 5, mentioned in your steps.

In case of Managed Package , could you show an example to do this ? and would this also be applicabe to Unmanaged Packages


James LoghryJames Loghry
So Managed Packages have to have what's called a namespace associated with them.  Unmanaged packages may or may not have a namespace depending on how they were set up.  The namespace in managed packages is unique to that package, and provides a reference for querying the packages fields, calling the packages global classes, etc etc.

If you look at the object detail or field detail, you should see information regarding the namespace, if there is one.

Here's an example I found in my dev org.  This managed package / custom field has a namespace of "pca"

To write a subquery to pull in related businesses for a parent Setting object, I would write the following SOQL, with the namespace followed by two underscores:

Select Id, (Select Id From pca__Businesses__r) From pca__Setting__c

Please note that the prefix__ is used primarily for code in an org where the managed package is installed.  I don't necessarily need to specify the prefix in an org where I am developing the managed package, for instance.


User-added image