• Rafsan Khan
  • 0 Points
  • Member since 2022

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 2
Hi All,
I am getting DUPLICATE_VALUE: the price definition already exist in the price book:[] error in test class. How do we resolve it?
I am getting above error when I use standard product Id in test class. Class is passing If I use custom pricebookId but failing while deploying.
Product2 pd = new Product2(Name='Product1',IsActive=true);
Insert pd;
Id standardPbId = Test.getStandardPricebookId();
PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPbId,IaActive=true,UseStandardPrice=false,Product2Id=pd.Id,UnitPrice=1200);
insert pbe; // this line is failing
If I use below custom price book Id Instead standardPbId then it is passing but failing while deploying it.
Pricebook2 customPb=  new Pricebook2(Name='sample price book',IsActive=true);
insert customPb;
I tried below work arounds but didn't work.
Pricebook2 pb2 = new Pricebook2 (Id=Test.getStandardPricebookId(),IsActive=true);
update pb2;
Id standardPbId =[SELECT Id FROM Pricebook2 where IsStandard=true LIMIT 1].Id;
Did any one got this error and resolve it. please provide the solution for this.