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
Tommy GeorgiouTommy Georgiou 

test class keep failing

My trigger is :
 
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?
Dushyant SonwarDushyant Sonwar
Hey Tommy,
Remove seaalldata=true
and try this
Test.getStandardPricebookId();
Hope this helps.
 
Tommy GeorgiouTommy Georgiou
Hi Dushyant,

Tried it and getting the following error message : System.QueryException: List has no rows for assignment to SObject
Tommy GeorgiouTommy Georgiou
Hi Dushyant,

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
Dushyant SonwarDushyant Sonwar
Hey Tommy,
Did You try this?
@isTest
   public class Test7{
    public static testmethod void TaskTrigger_Test1()
    {
            Account a = new Account(Name = 'Test');
            insert a;
                     
            
            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 = Test.getStandardPriceBookId(), Product2Id = prd1.Id, UnitPrice = 1, IsActive = true, UseStandardPrice = false);
            insert standardPrice;
             
            PricebookEntry pe=new PricebookEntry(UnitPrice = 1,Product2Id=prd1.id,Pricebook2Id=Test.getStandardPriceBookId(),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);
    }


}

 
Dushyant SonwarDushyant Sonwar
This will Surely Work.
Tommy GeorgiouTommy Georgiou
Hey Dushyant,

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: []
Dushyant SonwarDushyant Sonwar
Tommy,
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;
 
Tommy GeorgiouTommy Georgiou
Hi,

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]
Dushyant SonwarDushyant Sonwar
did you make UseStandardprice=true ?


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;
Tommy GeorgiouTommy Georgiou
Hi Dushyant,

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: []
 
Tommy GeorgiouTommy Georgiou
The error comes from the insert pe;