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
Abhilash Mishra 13Abhilash Mishra 13 

gettting all the products Of an order by SOQL

I want to get all the product associated with an order. like shown in order product related list. how can i do This.
Need urgent help thanks in advance.
Best Answer chosen by Abhilash Mishra 13
sachin kadian 5sachin kadian 5
Here is the full code
List<String> productNames = new List<String>();
for(order ord:[SELECT Id,name,(select Id,
       OrderId,   
       OrderItemNumber,
       PricebookEntry.Product2.Name,
       PricebookEntry.Product2.id,
       Quantity,
       UnitPrice
       FROM OrderItems ) from order where id='801280000000PTy']){
           for(OrderItem oi : ord.orderItems){
               productNames.add(oi.PricebookEntry.Product2.name);
           }    
}
System.debug(productNames);

All Answers

sachin kadian 5sachin kadian 5
Here is the full code
List<String> productNames = new List<String>();
for(order ord:[SELECT Id,name,(select Id,
       OrderId,   
       OrderItemNumber,
       PricebookEntry.Product2.Name,
       PricebookEntry.Product2.id,
       Quantity,
       UnitPrice
       FROM OrderItems ) from order where id='801280000000PTy']){
           for(OrderItem oi : ord.orderItems){
               productNames.add(oi.PricebookEntry.Product2.name);
           }    
}
System.debug(productNames);
This was selected as the best answer
Marlese Rosch HadenMarlese Rosch Haden
I tried this but get MALFORMED_QUERY: unexpected token: List