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.