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
Paulo PerezPaulo Perez 

Fail Test: DUPLICATE_VALUE in PricebookEntry

Hello,

Please, I've had trouble in PricebookEntry when I execute test below: 

@isTest 
(seeAllData = true)

private class TestOpportunityLineItem
{
static testMethod void TestProOportunidade()
{

Date closeDt = Date.Today();

date myDate = date.today();

Account a2 = new Account(Name ='Teste');
insert a2;

opportunity oppr = new opportunity(Name='Encomenda teste' ,  AccountId= a2.Id,StageName = 'Fechado', 
                                   CloseDate = closeDt);
insert oppr;

Id pricebookId = Test.getStandardPricebookId();

Product2 prod = new Product2(
     Name = 'Vidro',
     ProductCode = '1108',
     isActive = true
);
insert prod;

PricebookEntry pbEntry = new PricebookEntry(
     Pricebook2Id = pricebookId,
     Product2Id = prod.Id,
     UnitPrice = 200.00,
     //StandardPrice= 100.00,
     //UseStandardPrice = false,
     IsActive = true
);
insert pbEntry;

OpportunityLineItem oli = new OpportunityLineItem(
     OpportunityId = oppr.Id,
     Quantity = 5,
     PricebookEntryId = pbEntry.Id,
     TotalPrice = 1
);
insert oli;
 

}
}
Best Answer chosen by Paulo Perez
Pankaj_GanwaniPankaj_Ganwani
Just remove seealldata=true and then try to run.

All Answers

Pankaj_GanwaniPankaj_Ganwani
Just remove seealldata=true and then try to run.
This was selected as the best answer
Paulo PerezPaulo Perez
Ok! Pankaj_Ganwani, now changed the fail:
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AutoPopulatePricebookEntry: execution of AfterInsert

it was in line 29 when insert product:

Product2 prod = new Product2(
     Name = 'Vidro',
     ProductCode = '1108',
     isActive = true
);
insert prod;
 
Paulo PerezPaulo Perez
I solved. I've got other trigger that I changed for disable.