I've written a test class for order update, though I am stuck at some minor issue which I am unable to get through. My test class looks like:

public class TestCreateMyOrder {
    static testMethod void validateCreateMyOrder() {

        // for pricebook and product entry
        Pricebook2 standardPricebook = new Pricebook2(
            Id = Test.getStandardPricebookId(),
            IsActive = true
        update standardPricebook;
        standardPricebook = [SELECT IsStandard FROM Pricebook2 WHERE Id = :standardPricebook.Id];
        Product2 p = new Product2(Name='My Product', IsActive = true);
        insert p;
        PricebookEntry pbe = new PricebookEntry(UnitPrice=10.0, Product2Id=p.Id, Pricebook2Id=standardPricebook.Id, IsActive=true);
        insert pbe;

        //new order entry
        Order ord = new Order(
            Name='Test Order',
            AccountId='0018A00000HWF1YQAX',// this is an existing account id
            Pricebook2Id = standardPricebook.Id
        insert ord;

        //creating order item
        OrderItem oi = new OrderItem(
        insert oi;

        //update order status
        update ord;

Here, order item insertion is necessary as without any product in an order, the order status can not be set to activated. The error I am getting here is an exception thrown at the time on insert oi; :

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [PricebookEntryId]: [PricebookEntryId]

I hope to get some understanding of the error here


Pricebook entry is missing from test data. You need to create test Pricebook entry.

PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
stdPriceBookEntry.Product2Id=p.Id; //Product Id
insert stdPriceBookEntry;

Assign the price book entry to the OrderItem.

oi.PricebookEntryId = stdPriceBookEntry.id;

Order Item

Test Class

  • ok, I've already created a PricebookEntry so I can use Id of that only. I was unable to check this field in the object at the UI level. Needed to go through the workbench. Thanks – Anant Aug 20 '19 at 8:55
  • @Anant yes you can use that, also, you can view all fields of an object from setup whenever you don't find it in layout. – rahul gawale Aug 20 '19 at 9:05
  • yes, I've checked it in setup->object manger only, though OrderItem lists no field with name PricebookEntryId there. – Anant Aug 20 '19 at 9:08
  • @Anant, Ohh I see, another way and the best place is the documentation. Here you can find all info. – rahul gawale Aug 20 '19 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.