+ Start a Discussion
CRM JediCRM Jedi 

SOQL - 3 Level Query



Assuming that Contact has a relationship to Account, and Account has a relationship to Owner (User), how do I get the information of the Owner when I query from the Contact?


I've constructed the following query base on the example of the documentation, it doesn't throw any error but I couldn't get the values for the Owner:


SELECT Id, Name, Account.Owner.FirstName FROM Contact;



Is the SOQL correct?

OnDem DevOnDem Dev

Are you sure that the query you mentioned does not throw an error.

You have to refer Account .OwnerId.

Mostly you have to submit 2 queries to get rthe desired result.


Or you can create Custom User field(Lookup) on Account and populate it with the Account owner.

Then you can get the Firstname of Owner in a single query.


For ex,

If AccountOwner__c is custom lookup field.

Then you can query as below

select id, name, Account.AccountOwner__r.FirstName from Contact


Hope this helps.




That looks fine (it worked for me in SoqlX), how are you trying to access the results ?
CRM JediCRM Jedi

Hi Simon,

We would like to use it in Apex Trigger. I did my testing with System Log and Eclipse schema browser.


Should work fine, post your actual code if you're still having problems

Hi CRMJedi.


I have given a try for your Code, it is working fine.


Public Class SoqlError{

    Public void SoqlErr(){
    Contact C = [Select id, name, Account.Owner.FirstName From contact  a limit 1];
    system.debug('Working here'+ c.Account.Owner.FirstName );



Please post your actuall code.