1

I'm trying to access fields of the Product2 parent from the OrderItem and running into a strange error, "No such column 'Product2Id' on entity 'OrderItem'". The following string of code works when run from Execute Anonymous, but does not compile when written within an Apex Class (see screenshot below):

List<OrderItem> oiList = [SELECT Id, Product2Id, Product2.Name FROM OrderItem];

    for(OrderItem oi : oiList){
        System.debug(oi.Product2.Name);

The Product2 lookup field clearly exists and is documented, but I can't access it in my Apex code. On the other hand, a SOQL query of the Order with OrderItems works fine (see second screenshot). Kindly assist.

enter image description here

enter image description here

0

1 Answer 1

2

The Product2Id field and its relationship were introduced in a later API version than your class is set to (38.0). Try changing it to 57.0, and it should deploy.

2
  • Worked like a charm, thank you. Jan 23 at 1:26
  • @TonyKirumba Yeah, no problem. Glad I could help!
    – sfdcfox
    Jan 23 at 1:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .