function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MAITREYEE DINGARE 3MAITREYEE DINGARE 3 

Test Class for 'before insert' trigger of Create OrderItems

Hello All,
I have written a Trigger on OrderItems (before Insert) and now writing a test class for the same. 
I am getting an error as:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Pricebook Not Set on Order: []

My Trigger:
trigger UnitPrice on OrderItem (before insert) {

    for (OrderItem orderItem: Trigger.new) {
    if(orderItem.UnitPrice == NULL){
            orderItem.UnitPrice= 0; // Dummy Value
            }
    }

}

Test Class:
@isTest(SeeAllData=true)
public class testUnitPrice {
    
    public static testMethod void testorderproduct(){

    // Insert Account

    Account a = new Account();
    a.Name = 'Test Account';
    insert a;

    // Insert Product
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    

    Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
    
    // Insert PricebookEntry

    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = standardPb.Id;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    // Insert Order
    
    Order o = new Order();
    o.Name = 'Test Order ';
    o.Status = 'Draft';
    o.EffectiveDate = system.today();
    o.EndDate = system.today() + 4;
    o.AccountId = a.id;
    o.Pricebook2Id =  standardPb.Id ;
    insert o;
    
    // Insert Order Item

    OrderItem i = new OrderItem();
    i.OrderId = o.id;
    i.Quantity = 5;
    i.UnitPrice = 0;
    i.Product2id = p.id;
    i.PricebookEntryId=standardPrice.Id;
    insert i;

	}
}

I am very new to programming. Any help would be appreciated.

Thank you!
Maitreyee Dingare​​​​​​​​​​​​​​
Maharajan CMaharajan C
Hi Maitreyee,

Please try the below test class:
 
@isTest
public class UnitPriceOrderItemtest {
    
    public static testMethod void testorderproduct(){
        
        // Insert Account
        
        Account a = new Account();
        a.Name = 'Test Account';
        insert a;
        
        // Insert Product
        Product2 p = new Product2();
        p.Name = ' Test Product ';
        p.Description='Test Product Entry 1';
        p.productCode = 'ABC';
        p.isActive = true;
        insert p;
        
        
        // Insert PricebookEntry
        
        PricebookEntry standardPrice = new PricebookEntry();
        standardPrice.Pricebook2Id = Test.getStandardPricebookId();
            standardPrice.Product2Id = p.Id;
        standardPrice.UnitPrice = 1;
        standardPrice.IsActive = true;
        standardPrice.UseStandardPrice = false;
        insert standardPrice ;
        
        // Insert Order
        
        Order o = new Order();
        o.Name = 'Test Order ';
        o.Status = 'Draft';
        o.EffectiveDate = system.today();
        o.EndDate = system.today() + 4;
        o.AccountId = a.id;
        o.Pricebook2Id =  Test.getStandardPricebookId();
            insert o;
        
        // Insert Order Item
        
        OrderItem i = new OrderItem();
        i.OrderId = o.id;
        i.Quantity = 5;
        //i.UnitPrice = 0;
        i.Product2id = p.id;
        i.PricebookEntryId=standardPrice.Id;
        insert i;
    }
}

Thanks,
Maharajan.C
MAITREYEE DINGARE 3MAITREYEE DINGARE 3
Hello Maharajan,

Thank you for the reply. It is now giving me coverage 100%.
Unfortunately it is still failing with the same error as:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Pricebook Not Set on Order: []
Do you know where we are getting it wromg?
Thank you!
Maitreyee Dingare
Maharajan CMaharajan C
Hi Maitreyee ,

I hope you copied the same code above i used...  Because am not getting any failure...

If the below line is not there in test class then you will get this issue...
o.Pricebook2Id = Test.getStandardPricebookId();
insert o;

Please read the below link also:
https://salesforce.stackexchange.com/questions/49472/problem-with-inserting-a-new-order-item

Thanks,
Maharajan.C
MAITREYEE DINGAREMAITREYEE DINGARE
Hello Maharajan, yes I have copied the same code. 
We have renamed the object PriceBook to Listung. Is this causing any issue?

Thank you!