+ Start a Discussion
Erik RodgersErik Rodgers 

Help with SOQL

I'm attempting what I believe to be a fairly simple and straight-forward SOQL query to get fields from the Salesforce Order Product (OrderItem) standard object. I am also attempting to reference a couple fields on the Product (Product2) object that is related to Order Product via lookup field Product. Here is my query:

SELECT Id,
       OrderId,   
       OrderItemNumber,
       Product2.Name,
       Quantity,
       UnitPrice
  FROM OrderItem

All profiles has field level visibility off all fields, but the query errors with "INVALID FIELD - No such relation Product2 on entity OrderItem." What am I doing wrong. This field most certainly exists as a lookup to the Product object. The query runs fine if I take out "Product2.Name"
Himanshu ParasharHimanshu Parashar
Hi Erik,

There is no direct association between Order and Product2. it is always accessible throught Pricebookentry so following query will work for you.
 
SELECT Id,
       OrderId,   
       OrderItemNumber,
       PricebookEntry.Product2.Name,
       Quantity,
       UnitPrice
  FROM OrderItem

Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Erik RodgersErik Rodgers
It is not an association between “Order” and “Product” but rather “Order Product” and “Product,” and that association does exist.