You need to sign in to do that
Don't have an account?

test class keep failing
My trigger is :
my test class is :
And the error is : System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This price definition already exists in this price book: []
Any Ideas why?
trigger TaskTrigger on Task (after insert) { List<Order> ordersToUpdate = new List<Order>(); for(Task t : Trigger.new) { if(t.WhatId.getSObjectType() == Order.sObjectType && t.Subject.startsWith('Email:')) { ordersToUpdate.add(new Order(Id = t.WhatId, Status = 'Sent')); } } update ordersToUpdate; }
my test class is :
@isTest(seeAllData = true) public class Test7{ public static testmethod void TaskTrigger_Test1() { Account a = new Account(Name = 'Test'); insert a; //get standard pricebook Pricebook2 standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1]; Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1',productCode = 'ABC', isActive = true); insert prd1; PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = prd1.Id, UnitPrice = 1, IsActive = true, UseStandardPrice = false); insert standardPrice; PricebookEntry pe=new PricebookEntry(UnitPrice = 1,Product2Id=prd1.id,Pricebook2Id=standardPB.id,isActive=true, UseStandardPrice = false); insert pe; Order o = new Order(name='Test1',AccountId=a.id,EffectiveDate=system.today(),status='draft'); insert o; OrderItem oi = new OrderItem(OrderId=o.id,Quantity=decimal.valueof('1'),PricebookEntryId=pe.id); insert oi; Task t = new Task(whatid=o.id,Priority = 'normal',status='open',subject='Email:xxxx'); insert t; system.assertequals('Sent',o.status); } }
And the error is : System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This price definition already exists in this price book: []
Any Ideas why?
Remove seaalldata=true
and try this
Test.getStandardPricebookId();
Hope this helps.
Tried it and getting the following error message : System.QueryException: List has no rows for assignment to SObject
When I try this it wont let me save the class as the error is Error: Compile Error: Variable does not exist: standardPB.id at line 14 column 96
Did You try this?
Yes I have. Still getting System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This price definition already exists in this price book: []
Remove this Line as i don't think you are using it anywhere.
PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = Test.getStandardPriceBookId(), Product2Id = prd1.Id, UnitPrice = 1, IsActive = true, UseStandardPrice = false); insert standardPrice;
Deleted and then tried again. Error was System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Price Book Not Set on Order: []
Then I insert the Pricebook2Id=Test.getStandardPriceBookId() into the order and now getting System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: UnitPrice (unit price cannot be null): [UnitPrice]
PricebookEntry pe=new PricebookEntry(UnitPrice = 1,Product2Id=prd1.id,Pricebook2Id=standardPB.id,isActive=true, UseStandardPrice = true);
insert pe;
and remove this line
PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = Test.getStandardPriceBookId(), Product2Id = prd1.Id, UnitPrice = 1, IsActive = true, UseStandardPrice = false); insert standardPrice;
I had problems logging into the forums. Sorry for the delay in my response. I did change the UseStandardprice=true and removed the line that you suggested and my error now is : System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: []