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
TheRealMarchandojTheRealMarchandoj 

How to query OrderItems from Opportunity

I need to build a query which allows me to pull back, Opportunity Line Items, Opportunity, Order and Order Items.
I've started writing the query on Opportunity, but I cannot figure out how to query the Order Items, as I am still getting my head around SOQL.

Can someone help point me in the right direction?

Thanks
 
SELECT Id, Order__c, Order__r.CreatedDate, Owner.Name,                   
                     (SELECT Id, Training__c, Scrappage_Cost__c, Misc_Cost__c FROM OpportunityLineItems) FROM Opportunity WHERE RecordType.DeveloperName != 'CoolAir_Quote' AND Order__c != ''

 
pconpcon
You are headed the right direction.  You will want to use the relationship name [1] [2] and query based on that.
 
select Order__c,
    Order__r.CreatedDate,
    Owner.Name,
    (
        select Training__c,
            Scrappage_Cost__c,
            Misc_Cost__c
        from OpportunityLineItems
    ),
    (
        select ListPrice,
            ...
        from OrderItems
    )
from Opportunity
where RecordType.DeveloperName != 'CoolAir_Quote' and
    Order__c !=''

[1] http://salesforce.stackexchange.com/questions/47955/how-can-i-query-orders-to-get-orderitems-associated-with-that-order
[2] https://developer.salesforce.com/blogs/developer-relations/2015/02/using-metadata-api-describe-objects.html