+ Start a Discussion
Tim SauchukTim Sauchuk 

Unable to query lookup on custom User Object

I have a field in Account that is a type of User, called Customer_Support_User__c. I would like to query Accounts and get all of the "Name" field for all Customer_Support_User__c. I've tried the following:
- SELECT Customer_Support_User__c.Name from Account
- SELECT (SELECT Name FROM Customer_Support_User__c) FROM Account

The Customer_Support_User__c is the Parent of Account, so based on what I've read, the second query I tried should work. Any ideas?
a sangeetha 5a sangeetha 5
Hi Tim,

Please find the query below.
My understanding
Parent object (custom) (Customer_Support_User__c):
Child object (standard) (Account):

1. Use this below Query to get parent object (custom - Customer_Support_User__c) from child object (standard - Account)  
select id, tryagain__Customer_Support_User__r.id, tryagain__Customer_Support_User__r.name from account
see image for reference, here tryagain is my dev org namespace

User-added image

2. Use this below Query to get child object (standard - Account)  from parent object (custom - Customer_Support_User__c)
select id , (select id from tryagain__AccountChild__r) from tryagain__Customer_Support_User__c
Please note the __r in subquery and 'tryagain__AccountChild__r' is the child relationship name to account, which you can get from field definition of child object. see below image
User-added image


see image for reference, here tryagain is my dev org namespace

User-added image

Kindly mark this as solved if it's resolved so that it gets removed from the unanswered queue.

Thanks,
Sangeetha A
Tim SauchukTim Sauchuk
Hi Sangeetha A,

I got this to work, just with a slight tweek to your #1 answer. I removed the "try_again" from the start of the user reference:

select  id, Customer_Support_User__r.Name from account

Thanks for the help! I'm curious, where can I find the reference to Customer_Support_User__r in the Object Manager? All I could find was ​​​​​​​Customer_Support_User__c.

Best,
Tim
a sangeetha 5a sangeetha 5
Hi Tim,

Glad to know it helped. Please find the reference link for more understanding of relationship queries,
Relationship Queries Reference (https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_and_custom_objects.htm)
Kindly mark this as solved if it's resolved so that it gets removed from the unanswered queue.

Thanks,
Sangeetha A