Sometimes we get a requirement in which we need to create OrderItem programmatically and we need to research a lot to know the required fields of Order Item.
Decimal Quantity; Id orderId; // parent order id Id pbEntryId; // make query to get pb entry id Decimal unitprice; // can get it from PricebookEntry.UnitPrice OrderItem oi = new OrderItem(Quantity =Quantity, OrderId = orderId, PricebookEntryId = pbEntryId, UnitPrice = unitprice); insert oi;