Skip to main content
1 of 2
Anant
  • 133
  • 1
  • 9

Writing test class for order activate

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

Anant
  • 133
  • 1
  • 9