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
GobbledigookGobbledigook 

Querying SOQL with Lookup fields

How would I add in a lookup field to a SOQL query? 

 

More specifically, I'm running a query that returns results based on a lookup field itself.  Actually it's a lookup field to a lookup field, as odd as it may seem, so I can't just type Contact__r.Name for instance, and as far as I know I can't reference anything two levels deep, so Contact__r.Account__r.Name is out, or am I wrong about this? 

 

Or does anyone have any workarounds so I can get the Name field from the Account object from a child of the Contact object and include it into the filtering results of a SOQL call?

 

Example:  [select Id from Child_Of_Contact where Contact__r.Account__r.Name =:'Ted'];  

Best Answer chosen by Admin (Salesforce Developers) 
rmehrmeh

Hi,

 

I think the query mentioned by you should work.

Which version of Eclipse IDE are you using?

 

I ran the below query in my IDE and it is giving the required results.

 

Select Contact.Account.Name from Contact where Contact.Account.Name != null

 

Hope this helps!!!

All Answers

rmehrmeh

Hi,

 

I think the query mentioned by you should work.

Which version of Eclipse IDE are you using?

 

I ran the below query in my IDE and it is giving the required results.

 

Select Contact.Account.Name from Contact where Contact.Account.Name != null

 

Hope this helps!!!

This was selected as the best answer
GobbledigookGobbledigook

Yeah, I made a stupid mistake about the whole thing, and corrected it almost immediately after posting. 

 

Thanks for your reply though!