You need to sign in to do that
Don't have an account?
MAITREYEE 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:
My Trigger:
Test Class:
I am very new to programming. Any help would be appreciated.
Thank you!
Maitreyee Dingare
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
Please try the below test class:
Thanks,
Maharajan.C
Thank you for the reply. It is now giving me coverage 100%.
Unfortunately it is still failing with the same error as: Do you know where we are getting it wromg?
Thank you!
Maitreyee Dingare
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
We have renamed the object PriceBook to Listung. Is this causing any issue?
Thank you!