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
Tanner RussellTanner Russell 

Need help with nested queries

I am new to salesforce SOQL and I seem to be having an issue when i try to use nested queries the query i am trying to perform is 

List<Space__c> sp = [Select Name, Suite__c, (Select Name from Location__c), (Select Name from RecordType where Name = 'Commercial') from Space__c];

and i keep getting this error 

Name, Suite__c, (Select Name from Location__c), (Select Name from
                                  ^
ERROR at Row:1:Column:42
Didn't understand relationship 'Location__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.

even if i append __r instead of __c i still get the same issue any ideas will help i just need the names out of the 2 related objects
Best Answer chosen by Tanner Russell
Jasper WallJasper Wall
try the below query
 
select Id,(select Id from Spaces__r) from Location__c

Thanks,
Balayesu

All Answers

Jasper WallJasper Wall
Hi Russell,

in nested query you have to use the child relationship name of the lookup field of that object.
for example, 
 Location is a lookup field of the object Space, just open the details of this lookup field and find the relationship name , suppose itit is "Locations", then your query is like,

List<Space__c> sp = [Select Name, Suite__c, (Select Name from Locations) from Space__c];

Mark it as the best Answer if it helps.

Thanks,
Balayesu

 
pkpnairpkpnair

You have to provide the right name of your relationship object. There is an easy to find this. 

Logon to https://workbench.developerforce.com/login.php
Click on the Info-->Standard & Custom Objects
Choose an object to describe: Choose your parent object Space__c here
Drill down to Child relationship folder-->Choose your child object name
Then make use of the value in the field relationshipName:

This will solve this error.

Tanner RussellTanner Russell
Trying this since that what shows up under the relationship name but i a, still getting the error 
Select Name, Suite__c, (Select Name from Property_Name__r), RecordType from Space__c where RecordType.Name = 'Commercial'
ERROR at Row:1:Column:42

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

Note: Property_Name__c is the name of the custom lookup feild on space to location
also tried Child Relationship Name: Spaces
as this is what shows up when i open the custom feild
pkpnairpkpnair
Did you check the relashipName using Workbench?
Tanner RussellTanner Russell
yeah it wasnt under the child relationship folder for some reason i found it under the field dropdown
pkpnairpkpnair
Could you please provide a screen shop of parents object folder structure from the workbench? showing the chilrelationship names too? This error is obviously due to the wrong name
Tanner RussellTanner Russell
User-added image
pkpnairpkpnair
I do not see a child relationship named like Property_Name
Tanner RussellTanner Russell
yeah im not sure why yet on the object there is a location lookup but it does show up under fields
 
pkpnairpkpnair
Did you create a lookup relationship? Can you please check your object field definition once again?
Tanner RussellTanner Russell
User-added image
Jasper WallJasper Wall
try the below query
 
select Id,(select Id from Spaces__r) from Location__c

Thanks,
Balayesu
This was selected as the best answer
Tanner RussellTanner Russell
that query worked it returned the space id and the location id
pkpnairpkpnair
The screenshot shows that your Child relationship is Spaces related to the object Location.
Property_Name is a field not the object name
Tanner RussellTanner Russell
ohh okay im pretty new to the system i didnt realize it was the child 
Jasper WallJasper Wall
Mark any answer that helped you as the best answer to help some one.
Tanner RussellTanner Russell
Thank you all for the support the final query I came up with that solves my problem is 
select Id, Name, (select Name, Suite__c, RecordType.Name from Spaces__r) from Location__c where RecordType.Name = 'Commercial'