+ Start a Discussion
Wei Cheng Hsu 9Wei Cheng Hsu 9 

get parent values inside a set

Account has a field `Source_System__c` as its parent.

I wanna query Name, Id from Source_System

This query is workable. 
[SELECT Source_System__r.Name, Source_System__r.Id from Account]

However, I wanna have the value from Source_System can be nested in a object.
Like the sub-query's result. But it didn't work.



    SELECT (SELECT Name, Id from Source_System) FROM Account
                             ^
ERROR at Row:1:Column:30
Didn't understand relationship 'Source_System' 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.



    SELECT (SELECT Name, Id from Source_System__r) FROM Account
                             ^
ERROR at Row:1:Column:30
Didn't understand relationship 'Source_System__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.
Ankur Saini 9Ankur Saini 9
Hi Wei Cheng Hsu

Source_System__c is a parent object and Account is a child object.
If you want to query on child object use : 

[SELECT (SELECT Name, Id from Account) FROM Source_System__c]

If you want to query on parent object use :

[SELECT Source_System__r.Name, Source_System__r.Id from Account]


Thanks 
Ankur Saini
sachin sabusachin sabu
Hi Wei Cheng Hsu,

Try this
[select (select name from  Accounts__r)from Source_System__c];

 
Nitish TalekarNitish Talekar
Traversing from Child to parent we say it as Upwards traversal, then use :
[SELECT Id, Account.Name, Account.Description FROM Contact ]

for more details check : Simple Guidance For You In Access Of Subquery Field Value Using Apex - Upwards Traversal (http://howtodoitinsalesforce.blogspot.in/2016/12/access-subquery-field-value-using-apex_4.html

For nested query you have to use the plural version “Contacts” in the nested SOQL query. Find the keyword of a custom relationship from the lookup or master-detail field.

Traversing from parent to Child we say it as Downwards traversal, then use :
[select id, Name, (select id, Name, Email from Contacts) from Accoun]

for more details check : Access Subquery Field Value Using Apex in 2 Easy Steps- Downwards Traversal (http://howtodoitinsalesforce.blogspot.in/2016/12/access-subquery-field-value-using-apex.htm)​ 

Please mark as best answer if it works for you.

Thanks,
Nitish