I am trying to write a SOQL query to return some Lead data, along with the Lead Owner's CompanyName.

I have tried multiple iterations, but cannot seem to get the syntax right.

Something like this:

[select Id, Name, OwnerId, (select Id, CompanyName from User where Id =: OwnerId) from Lead limit 10]


When you need to traverse through a reference field, you can use a dot operator to get the related object's fields. In this case this would be Owner.CompanyName because this is a standard reference field.

Similarly lets say if this would have been a custom reference field called Owner__c on Lead; with the relationship name as Owner__r, syntax to get the reference field would have beeen Owner__r.CompanyName

The actual query would look like below-

SELECT Id, Name, OwnerId, Owner.CompanyName From Lead LIMIT 10

The query you are trying to write is used to retrieve child object's records along with the record of current object.

  • 1
    It isn't necessarily a "parent", by the way, but just a referenced object using a Lookup field. This only is a "parent" for a Master Detail relationship.
    – Phil W
    Aug 3 '20 at 20:48
  • @PhilW you're right, thanks for the correction, I have amended the answer.
    – Raul
    Aug 3 '20 at 20:51
  • Unfortunately not all Owner fields can be referenced via a dot operator. CompanyName is one that cannot. salesforce.stackexchange.com/questions/47467/…
    – Bryan Hunt
    Aug 4 '20 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.