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
irfan azizirfan aziz 

How to query a lookup field?

I have a field owner(data type lookup(user)) in the opportunity table.
SELECT createddate,closedate,owner from Opportunity limit 10
The above select query fails and says there is no such column owner.  Can anyone help.
Balayesu ChilakalapudiBalayesu Chilakalapudi
Try like this,
SELECT createddate,closedate,ownerId from Opportunity limit 10
irfan azizirfan aziz
Thank you Balayesu, Why does it not show as ownerId in field name.
Balayesu ChilakalapudiBalayesu Chilakalapudi
On salesforce UI, owner shows the Name of the owner but it is only accessible with owner.Name in soql

Your query will be like
select createddate,closedate,owner.Name,ownerId from opportunity limit 1
Erik LeirenErik Leiren

I have a similar situation, and I am not positive I understand the answer given here. I am setting up Talkdesk for Salesforce so that a call coming in will route to the account manager. I can successfully input the phone number to SF, and I can return fields that are not of a lookup type, but I fail to return wwith lookup field types. To make it simple, let's look up the account owner and a few other pieces of info:

SELECT OwnerID,AnnualRevenue,CreatedDate FROM Account WHERE Phone LIKE {input from Talkdesk from Salesforce}

I can set up an audio output in Talkdesk to give me the results, but right now it only returns the values for annual revenue and reated date. When it tries to return the OwnerID, it's just selence. 

Once this query successfully returns the OwnerID, I need to run a second query to lookup that OwnerID in the Contact object, and return their email address. 

I need to get the owner ID out of the first query such that I can use it again to search in the second query. What am I missing here? Why are all the lookup type fields failing to return results for me?