I have a custom object called "Department" and the following custom fields
- Department_Head__c – Lookup(User)
- Department_Name_cc – Text (80)
I'm trying to query out the department head's user information via the following query
select Department__c.Id, Department.User.Id from Department__c
Or
select Department__c.Id, (select User.Id, User.Username from Users) from Department__c
Both return the following error message:
Didn't understand relationship 'Department__c' in field path. 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.
Best Answer
If you need the id, just type [Select Id...], without Department__c. Same for all the other fields, except lookups used as relationships.
To get the user ID you need to use the user lookup API Name, for the custom fields you created on Department__c.
Id is the departments Id, Department_Head__c is the user id and you will get the users Name using Department_Head__r.Name. For every field from the user you will need to use the Department_Head__r as a relationship, except the ID, as shown in the example.