Search code examples
subquerysalesforcesoql

How to create subquery in SOQL, Opportunity - User tables


I have a Salesforce query which is pulling all the necessary columns from Opportunity table. It is something like that

Select o.Opportunity_ID__c, o.LastModifiedDate, o.Opportunity_Currency__c, o.Opportunity_Type__c 
FROM Opportunity o
ORDER BY o.LastModifiedDate ASC

All I need is to add one more column. This column is called "EmployeeNumber" in "User" table.

When I loo at Opportunity fields, there is a lookup which is pulling this data called "Opportunity Owner" field in "Opportunity" table.

I tried couple of sub queries which didn't work. Some of the solutions I have seen on Google search; they say, look for Child Relationships. I did but there is no such a child relationship saying "User" or "Owner" under Opportunity child relationship (I used Eclipse for that)

Can you please help me? Thank you


Solution

  • You should be able to use SOQL-R to follow the Owner relationship, e.g.

    Select Opportunity_ID__c, Owner.EmployeeNumber, .... From Opportunity ...
    

    This won't work if Owner if polymorphic (it can point to multiple types) but from what i remember Oppty owner is a isn't one of those.