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
Rodrigo_RochaRodrigo_Rocha 

SOQL query to retrieve custom field from record owner.

Hi all,

We have a custom field in the User object called Position__c. My goal is to write a query on the LEAD object and include this (maybe more) custom fields from the USER (Owner) object.

I know I can write a query like: "SELECT Id, Owner.Name FROM Leads" and that would return the owner name, but when I try to use Owner.Position__c I get an error.

Through a custom report type, custom fields (via lookup) from the User (Owner) isn't available. I could pottentialy create a custom formula field on the Lead object to retrieve this data and use it in a report, but we are trying to accomplish this without creating a new custom formula field.

Eventually I would need to group this report (query) like:

Position
    |> Onwer Name
        |> Lead Name


Any idea?

Thanks,
Rodrigo
HARSHIL U PARIKHHARSHIL U PARIKH
It will not allow us to query User Object fields from Leads or from several other objects.

If you don't want to create additional fields (like you have mentioned above), I would recommend following code:
 
Public void LeadToUser()
    {
    	List<Lead> leads = [Select Id, Owner.Name, OwnerId FROM Lead];
        List<Id> userIds = New List<Id>();
        
        For(Lead EveryLead : leads)
        {
            userIds.add(EveryLead.OwnerId);
        }
        
        List<User> userRecords = [Select Id, Position__c FROM User WHERE Id =: userIds];
    
    }
Hope this helps!