+ Start a Discussion
Wil PhoWil Pho 

accessing parent object using soql

Hi there,

New to writing SOQL scripts using Workbench. So there is a customer object called Affiliation (NU__Affiliation__r) that has a lookup relationship to the Account object.

SELECT Name,
       NU__Account__c,
       NU__ParentAccount__c
       Account.Name
FROM NU__Affiliation__c 


When I run the above script it's throwing the following error. I'm trying to access the parent Account fields. 

MALFORMED_QUERY: only aggregate expressions use field aliasing 

I also tried this script:

SELECT AccountNumber,Id,

(SELECT Name,NU__Account__c,NU__IsPrimary__c

FROM NU__Affiliation__r)

FROM Account

And I got this error:

INVALID_TYPE:
FROM NU__Affiliation__r)
^
ERROR at Row:5:Column:6
Didn't understand relationship 'NU__Affiliation__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.


Would very much appreciate any help!

Regards,
Wil
Best Answer chosen by Wil Pho
ayu sharma devayu sharma dev
Hello Wil,

In the first Query I think there can be two problems:
SELECT Name,
       NU__Account__c,
       NU__ParentAccount__c //Add a comma here
       Account.Name //I believe this should be NU__Account__r.Name
FROM NU__Affiliation__c

In the second query you can try below mentioned changes
SELECT AccountNumber,Id,

(SELECT Name,NU__Account__c,NU__IsPrimary__c

FROM NU__Affiliation__r) /*Instead of using NU__Affiliation__r try NU__Affiliations__r. If this does not work then try to find the correct child relation name by going to the Account Lookup field on the Affilication object and find the child Relation name there. Dont forget to add NU__ and __r Post and prefix to it.*/

FROM Account

Let me know if it helps of if any error occures.

Thanks,
​​​​​​​Ayush Sharma

All Answers

ayu sharma devayu sharma dev
Hello Wil,

In the first Query I think there can be two problems:
SELECT Name,
       NU__Account__c,
       NU__ParentAccount__c //Add a comma here
       Account.Name //I believe this should be NU__Account__r.Name
FROM NU__Affiliation__c

In the second query you can try below mentioned changes
SELECT AccountNumber,Id,

(SELECT Name,NU__Account__c,NU__IsPrimary__c

FROM NU__Affiliation__r) /*Instead of using NU__Affiliation__r try NU__Affiliations__r. If this does not work then try to find the correct child relation name by going to the Account Lookup field on the Affilication object and find the child Relation name there. Dont forget to add NU__ and __r Post and prefix to it.*/

FROM Account

Let me know if it helps of if any error occures.

Thanks,
​​​​​​​Ayush Sharma
This was selected as the best answer
Wil PhoWil Pho
Hi Ayush,

When I tried "NU__Affiliations__r" it worked!

Thank you so much!

Wil
ayu sharma devayu sharma dev
Great!! Please also mark it as Solved. Thanks.