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
The new LearnerThe new Learner 

DUPLICATE_VALUE, This price definition already exists in this price book:

Hi Experts,

Can anyone helps me , while running my test class i am getting below error. Below is my code anyone help  me please. the area which i made bold in the insert command i receiving this can anyone helps me please.

DUPLICATE_VALUE, This price definition already exists in this price book: []   


Account a = new Account(Name='testAccount',RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Customer').getRecordTypeId()); //Added by Purnima for Story S-562343
         insert a; 
        
        Id pricebookId = Test.getStandardPricebookId();
        
        Product2 prod = new Product2(Name = 'Laptop X200', 
                                     Family = 'Hardware');
        insert prod;
        
     
        
        /*PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;*/
        
        PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
        stdPriceBookEntry.Product2Id=prod.Id;
        stdPriceBookEntry.Pricebook2Id=pricebookId;
        stdPriceBookEntry.UnitPrice=2000;
        stdPriceBookEntry.IsActive=true;
        insert stdPriceBookEntry;

        
        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;
        
        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = prod.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;
         Product_Type__c productType = new Product_Type__c();        
        productType.Product_Type_Code__c = 'End';
      
        insert productType;
        
        
        Opportunity opp = new Opportunity(Name='Test', 
                                          Account = a, 
                                         // Product__c='GRI Retail',
                                          NextStep='test',
                                          PriceBook2 = customPB,
                                          CloseDate = System.today(),
                                          AccountId=a.Id, 
                                         // Opportunity_Name__c='testOpp',
                                         // Product_Family__c='Retail',
                                        Industry__c='Other',
                                        Description='TestTestTest',
                                          StageName='Closed Won - One Time' 
                                          );
       
       insert opp; 
        
    
     
    List<Opportunity_Product__c> oppProdList = new List<Opportunity_Product__c>();
     for(Integer i=1;i<=10;i++)
     oppProdList.add(new Opportunity_Product__c (Opportunity__c = opp.Id,Product__c = productType.id));
     
     System.debug('test@@@'+ oppProdList );
        
        insert oppProdList;
        
        

     
     
     ApexPages.StandardSetController sc = new ApexPages.StandardSetController(oppProdList);
    
     
    
      PageReference pageRef = Page.OpportunityProducts;
        pageRef.getParameters().put('id', String.valueOf(''));
        Test.setCurrentPage(pageRef);
    
    
    //Test.setCurrentPageReference(Page.OpportunityProducts);
    
    OpportunityController obj= new OpportunityController(sc);
    obj.selectedRowNum=1;
    obj.selectedRowProdId=productType.id;
    obj.selectedRowProdName=productType.Name;
    obj.cOpp=opp;
    List<Opportunity_Product__c> testopp= new List<Opportunity_Product__c>();
    obj.clstOppProds=oppProdList;
    List<Opportunity_Product__c> testOpps= new  List<Opportunity_Product__c>();
    obj.clstDelOppProds=oppProdList;
    
    obj.getJDEProductRTId();
   
    //Product_Type__c pt = new Product_Type__c(id='test', name='test');
    System.debug('clstOppProds==>' + obj.clstOppProds);
        System.debug('clstDelOppProds==>' + obj.clstDelOppProds);
    
    PageReference pg = obj.doSave(); 
    PageReference pg1 = obj.addNew(); 
    //PageReference pg3 = obj.setProd();
    PageReference pg2 = obj.removeRow(); 
    
 
Raj VakatiRaj Vakati
Try this  .. Looks like you have already Custom Pricebook in the application 
 
Account a = new Account(Name='testAccount',RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Customer').getRecordTypeId()); //Added by Purnima for Story S-562343
         insert a; 
        
        Id pricebookId = Test.getStandardPricebookId();
        
        Product2 prod = new Product2(Name = 'Laptop X200', 
                                     Family = 'Hardware');
        insert prod;
        
     
        
        /*PricebookEntry standardPrice = new PricebookEntry(
            Pricebook2Id = pricebookId, Product2Id = prod.Id,
            UnitPrice = 10000, IsActive = true);
        insert standardPrice;*/
        
        PriceBookEntry stdPriceBookEntry = new PriceBookEntry();
        stdPriceBookEntry.Product2Id=prod.Id;
        stdPriceBookEntry.Pricebook2Id=pricebookId;
        stdPriceBookEntry.UnitPrice=2000;
        stdPriceBookEntry.IsActive=true;
        insert stdPriceBookEntry;
        
        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook Test Class', isActive=true);
        insert customPB;
        
        PricebookEntry customPrice = new PricebookEntry(
            Pricebook2Id = customPB.Id, Product2Id = prod.Id,
            UnitPrice = 12000, IsActive = true);
        insert customPrice;
         Product_Type__c productType = new Product_Type__c();        
        productType.Product_Type_Code__c = 'End';
      
        insert productType;
        
        
        Opportunity opp = new Opportunity(Name='Test', 
                                          Account = a, 
                                         // Product__c='GRI Retail',
                                          NextStep='test',
                                          PriceBook2 = customPB,
                                          CloseDate = System.today(),
                                          AccountId=a.Id, 
                                         // Opportunity_Name__c='testOpp',
                                         // Product_Family__c='Retail',
                                        Industry__c='Other',
                                        Description='TestTestTest',
                                          StageName='Closed Won - One Time' 
                                          );
       
       insert opp; 
        
    
     
    List<Opportunity_Product__c> oppProdList = new List<Opportunity_Product__c>();
     for(Integer i=1;i<=10;i++)
     oppProdList.add(new Opportunity_Product__c (Opportunity__c = opp.Id,Product__c = productType.id));
     
     System.debug('test@@@'+ oppProdList );
        
        insert oppProdList;
        
        

     
     
     ApexPages.StandardSetController sc = new ApexPages.StandardSetController(oppProdList);
    
     
    
      PageReference pageRef = Page.OpportunityProducts;
        pageRef.getParameters().put('id', String.valueOf(''));
        Test.setCurrentPage(pageRef);
    
    
    //Test.setCurrentPageReference(Page.OpportunityProducts);
    
    OpportunityController obj= new OpportunityController(sc);
    obj.selectedRowNum=1;
    obj.selectedRowProdId=productType.id;
    obj.selectedRowProdName=productType.Name;
    obj.cOpp=opp;
    List<Opportunity_Product__c> testopp= new List<Opportunity_Product__c>();
    obj.clstOppProds=oppProdList;
    List<Opportunity_Product__c> testOpps= new  List<Opportunity_Product__c>();
    obj.clstDelOppProds=oppProdList;
    
    obj.getJDEProductRTId();
   
    //Product_Type__c pt = new Product_Type__c(id='test', name='test');
    System.debug('clstOppProds==>' + obj.clstOppProds);
        System.debug('clstDelOppProds==>' + obj.clstDelOppProds);
    
    PageReference pg = obj.doSave(); 
    PageReference pg1 = obj.addNew(); 
    //PageReference pg3 = obj.setProd();
    PageReference pg2 = obj.removeRow();

 
The new LearnerThe new Learner
Hi Raj,

I am facing same issue again on the same line.
Raj VakatiRaj Vakati
Can you check in the your controller .. is there any where your are passing the name for price book 
The new LearnerThe new Learner
HI Raj,

I dont see any kind of pricebook name is passing.