0

Hi all I have a test method which it creates data (makeData) for the following test methods.

But I'm getting this message over and over again.

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Price Book Entry ID: id value of incorrect type: 01s5e000006MreIAAS: [PricebookEntryId] Class.cmpHelperClassTest.makeData: line 50, column 1

Here is the code:

@TestSetup
    static void makeData(){
        Account acc = new Account();
        acc.Name = 'Testing Account';
        acc.ShippingStreet = 'Street Test';
        acc.ShippingPostalCode = 'PC Test';
        acc.Type = 'Consumer';
        Insert acc;    

        Contact cont = new Contact();
        cont.FirstName = 'First Name Test';
        cont.LastName = 'Last Name Test';
        cont.Salutation = 'Mr';
        cont.AccountId = acc.Id;
        cont.Email = 'emailTest@yopmail.com';
        insert cont;

        Order orden = new Order();
        orden.AccountId = acc.Id;
        orden.Status = 'draft';
        orden.EffectiveDate = Date.today();
        insert orden;

        Product2 product = new Product2();
        product.Name = 'TEST';
        product.ProductCode = 'Card';
        insert product;

        PricebookEntry price = new PricebookEntry();
        price.UnitPrice = 5000.0;
        price.Pricebook2Id = Test.getStandardPricebookId();
        price.Product2Id = product.Id;
        price.isActive = true;
        insert price;

        OrderItem orderItems = new OrderItem();
        orderItems.Product2Id = product.Id;  
        orderItems.OrderId = orden.Id;
        orderItems.Quantity = 1; 
        orderItems.UnitPrice= price.UnitPrice;
        orderItems.PricebookEntryId = price.Id;
        insert orderItems;
    }
6
  • 1
    What's line 50 in your code?
    – sfdcfox
    Jun 3 at 14:31
  • this is the line orderItems.PricebookEntryId = price.Id; Jun 3 at 14:40
  • @PabloCalderón PricebookEntry price = new PricebookEntry(); price. = 5000.0; you have missed the field name after your variable name. Jun 3 at 17:05
  • @ShagunSharma Hi Shagun, fixed that but still receiving the same message. Jun 3 at 17:21
  • Also, on order you need to set pricebook2Id it is mandatory field orden.Pricebook2Id=Test.getStandardPricebookId(); Jun 3 at 18:03
0

Your issue is as follows

Assuming you have set orden.Pricebook2Id = Test.getStandardPricebookId();

then when you insert the OrderItems, you need to omit the Product2Id as SFDC will default this from the OrderItem.PricebookEntry.Product2 relationship

OrderItem orderItems = new OrderItem();
  //  orderItems.Product2Id = product.Id;  <== remove this line
    orderItems.OrderId = orden.Id;
    orderItems.Quantity = 1; 
    orderItems.UnitPrice= price.UnitPrice;
    orderItems.PricebookEntryId = price.Id;
    insert orderItems;

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.