0

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:

@IsTest
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
            EffectiveDate=System.today(),
            Status='Draft',
            Pricebook2Id = standardPricebook.Id
        );
        insert ord;

        //creating order item
        OrderItem oi = new OrderItem(
            Product2Id=p.Id,
            Quantity=1.0,
            OrderId=ord.Id,
            UnitPrice=10.0
        );
        insert oi;

        //update order status
        ord.Status='Activated';
        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

1 Answer 1

5

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

PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
stdPriceBookEntry.Product2Id=p.Id; //Product Id
stdPriceBookEntry.Pricebook2Id=Test.getStandardPricebookId();;
stdPriceBookEntry.UnitPrice=2000;
stdPriceBookEntry.IsActive=true;
insert stdPriceBookEntry;

Assign the price book entry to the OrderItem.

oi.PricebookEntryId = stdPriceBookEntry.id;

Order Item

Test Class

4
  • 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
    Commented Aug 20, 2019 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. Commented Aug 20, 2019 at 9:05
  • yes, I've checked it in setup->object manger only, though OrderItem lists no field with name PricebookEntryId there.
    – Anant
    Commented Aug 20, 2019 at 9:08
  • @Anant, Ohh I see, another way and the best place is the documentation. Here you can find all info. Commented Aug 20, 2019 at 9:19

You must log in to answer this question.

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