0

please see starred(***) part in both method, I am getting error while deploying in production not in sandbox

@isTest public class DuplicateProduct_Test {
   public static testMethod void callMeth()
    {
        account acc = new account(name ='Code test', type = 'Carrier', RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Carrier Account').getRecordTypeId()); 
        insert acc;

        // 1st product
        product2 p = new product2(name='Pen Code Test', isActive=true, Carrier_Account__c = acc.Id);
        insert p;
        Opportunity opp = new Opportunity(AccountId=acc.Id, Name= 'opp Code Test 11/6 1', CloseDate= Date.today().addDays(30), StageName = 'Closed Won');
        insert opp;
        Opportunity opp2 = new Opportunity(AccountId=acc.Id, Name= 'opp Code Test 11/6 2', CloseDate= Date.today().addDays(30), StageName = 'Closed Won');
        insert opp2;
        Opportunity opp3 = new Opportunity(AccountId=acc.Id, Name= 'opp Code Test 11/6 3', CloseDate= Date.today().addDays(30), StageName = 'Closed Won');
        insert opp3;
        Id sPBId = System.Test.getStandardPricebookId();
        ***PricebookEntry sPBE = new PricebookEntry(UnitPrice=4000, isActive=true, Pricebook2ID=sPBId, product2Id=p.Id, UseStandardPrice = false);
        insert sPBE;***
            pricebook2 pB = new pricebook2(name='Old Code Test 11/6 1', isActive=true);
        insert pB;
        pricebook2 pB2 = new pricebook2(name='Old Code Test 11/6 2', isActive=true);
        insert pB2;
        pricebook2 pB3 = new pricebook2(name='Old Code Test 11/6 3', isActive=true);
        insert pB3;

        PricebookEntry pBE= new pricebookentry(unitprice=18, UseStandardPrice=false, isActive=true, pricebook2Id=pB.Id, product2Id=p.Id);
        insert pBE;
        PricebookEntry pBE2= new pricebookentry(unitprice=28, UseStandardPrice=false, isActive=true, pricebook2Id=pB2.Id, product2Id=p.Id);
        insert pBE2;
        PricebookEntry pBE3= new pricebookentry(unitprice=38, UseStandardPrice=false, isActive=true, pricebook2Id=pB3.Id, product2Id=p.Id);
        insert pBE3;

        OpportunityLineItem oli1 = new OpportunityLineItem(OpportunityId = opp.id, PricebookEntryId = pBE.Id, Product2Id = p.Id, Quantity = 10, UnitPrice=19);
        insert oli1;    
        OpportunityLineItem oli2 = new OpportunityLineItem(OpportunityId = opp2.id, PricebookEntryId = pBE2.Id, Product2Id = p.Id, Quantity = 20, UnitPrice=29);
        insert oli2;
        OpportunityLineItem oli3 = new OpportunityLineItem(OpportunityId = opp3.id, PricebookEntryId = pBE3.Id, Product2Id = p.Id, Quantity = 30, UnitPrice=39);
        insert oli3;

        // 2nd product

        product2 p2 = new product2(name='Pen Code Test 11/6 21', isActive=true, Carrier_Account__c = acc.Id);
        insert p2; 
        Opportunity opp4 = new Opportunity(AccountId=acc.Id, Name= 'opp Code Test 11/6 21', CloseDate= Date.today().addDays(30), StageName = 'Closed Won');
        insert opp4;             
        system.debug('opp Id '+opp4.Id+' ,'+opp3.Id+' ,'+opp2.Id+' ,'+opp.Id);
        Id sPBId2 = system.Test.getStandardPricebookId();
        PricebookEntry sPBE2 = new PricebookEntry(UnitPrice=40,  isActive=true, Pricebook2ID=sPBId2, product2Id=p2.Id);
        insert sPBE2;           
        system.debug('sPBE2 Id '+sPBE2.Id+' sPBE Id '+sPBE.Id);     
        pricebook2 pB4 = new pricebook2(name='Old Code Test 11/6 4', isActive=true);
        insert pB4;
        PricebookEntry pBE4= new pricebookentry(unitprice=48, UseStandardPrice=false, isActive=true, pricebook2Id=pB4.Id, product2Id=p2.Id);
        insert pBE4;

        OpportunityLineItem oli4 = new OpportunityLineItem(OpportunityId = opp4.id, PricebookEntryId = pBE4.Id, Product2Id = p2.Id, Quantity = 4, UnitPrice=49);
        insert oli4;   
        Test.StartTest();
        DuplicateProduct  testPlan = new DuplicateProduct();
        testPlan.noOfRow = 1;
        testPlan.addRow();
        List<DuplicateProduct.WrapperCls> wrpLt1 = new List<DuplicateProduct.WrapperCls>();
        wrpLt1.add(new DuplicateProduct.WrapperCls(1,1,p.Id,p2.Id));
        wrpLt1.add(new DuplicateProduct.WrapperCls(2,2,p2.Id,p.Id));
        testPlan.wrpClassList = wrpLt1;
        testPlan.deleteProduct();
        Test.stopTest();
    }

    public static testMethod void callMeth1()
    {
        account acc = new account(name ='Code test 16/6 1', type = 'Carrier', RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Carrier Account').getRecordTypeId()); 
        insert acc;

        // 1st product
        product2 p = new product2(name='Pen Code Test 17/6 1', isActive=true, Carrier_Account__c = acc.Id);
        insert p;
        Opportunity opp = new Opportunity(AccountId=acc.Id, Name= 'opp Code Test 11/6 1', CloseDate= Date.today().addDays(30), StageName = 'Closed Won');
        insert opp;
        Opportunity opp2 = new Opportunity(AccountId=acc.Id, Name= 'opp Code Test 11/6 2', CloseDate= Date.today().addDays(30), StageName = 'Closed Won');
        insert opp2;
        Opportunity opp3 = new Opportunity(AccountId=acc.Id, Name= 'opp Code Test 11/6 3', CloseDate= Date.today().addDays(30), StageName = 'Closed Won');
        insert opp3;
        Id sPBId = System.Test.getStandardPricebookId();
        ***PricebookEntry sPBE = new PricebookEntry(UnitPrice=1000, isActive=true, Pricebook2ID=sPBId, product2Id=p.Id, UseStandardPrice = false);
        insert sPBE;***
            pricebook2 pB = new pricebook2(name='Old Code Test 11/6 1', isActive=true);
        insert pB;
        pricebook2 pB2 = new pricebook2(name='Old Code Test 11/6 2', isActive=true);
        insert pB2;
        pricebook2 pB3 = new pricebook2(name='Old Code Test 11/6 3', isActive=true);
        insert pB3;

        PricebookEntry pBE= new pricebookentry(unitprice=18, UseStandardPrice=false, isActive=true, pricebook2Id=pB.Id, product2Id=p.Id);
        insert pBE;
        PricebookEntry pBE2= new pricebookentry(unitprice=28, UseStandardPrice=false, isActive=true, pricebook2Id=pB2.Id, product2Id=p.Id);
        insert pBE2;
        PricebookEntry pBE3= new pricebookentry(unitprice=38, UseStandardPrice=false, isActive=true, pricebook2Id=pB3.Id, product2Id=p.Id);
        insert pBE3;

        OpportunityLineItem oli1 = new OpportunityLineItem(OpportunityId = opp.id, PricebookEntryId = pBE.Id, Product2Id = p.Id, Quantity = 10, UnitPrice=19);
        insert oli1;    
        OpportunityLineItem oli2 = new OpportunityLineItem(OpportunityId = opp2.id, PricebookEntryId = pBE2.Id, Product2Id = p.Id, Quantity = 20, UnitPrice=29);
        insert oli2;
        OpportunityLineItem oli3 = new OpportunityLineItem(OpportunityId = opp3.id, PricebookEntryId = pBE3.Id, Product2Id = p.Id, Quantity = 30, UnitPrice=39);
        insert oli3;

        // 2nd product

        product2 p2 = new product2(name='Pen Code Test 11/6 21', isActive=true, Carrier_Account__c = acc.Id);
        insert p2; 
        Opportunity opp4 = new Opportunity(AccountId=acc.Id, Name= 'opp Code Test 11/6 21', CloseDate= Date.today().addDays(30), StageName = 'Closed Won');
        insert opp4;             
        system.debug('opp Id '+opp4.Id+' ,'+opp3.Id+' ,'+opp2.Id+' ,'+opp.Id);
        Id sPBId2 = system.Test.getStandardPricebookId();
        PricebookEntry sPBE2 = new PricebookEntry(UnitPrice=40,  isActive=true, Pricebook2ID=sPBId2, product2Id=p2.Id);
        insert sPBE2;           
        system.debug('sPBE2 Id '+sPBE2.Id+' sPBE Id '+sPBE.Id);     
        pricebook2 pB4 = new pricebook2(name='Old Code Test 11/6 4', isActive=true);
        insert pB4;
        PricebookEntry pBE4= new pricebookentry(unitprice=48, UseStandardPrice=false, isActive=true, pricebook2Id=pB4.Id, product2Id=p2.Id);
        insert pBE4;

        OpportunityLineItem oli4 = new OpportunityLineItem(OpportunityId = opp4.id, PricebookEntryId = pBE4.Id, Product2Id = p2.Id, Quantity = 4, UnitPrice=49);
        insert oli4;   
        Test.StartTest();
        DuplicateProduct  testPlan = new DuplicateProduct();
        testPlan.noOfRow = 12;
        testPlan.addRow();
        List<DuplicateProduct.WrapperCls> wrpLt1 = new List<DuplicateProduct.WrapperCls>();
        wrpLt1.add(new DuplicateProduct.WrapperCls(1,1,p2.Id,p.Id));
        wrpLt1.add(new DuplicateProduct.WrapperCls(2,2,p2.Id,''));
        wrpLt1.add(new DuplicateProduct.WrapperCls(1,3,'',p.Id));
        wrpLt1.add(new DuplicateProduct.WrapperCls(2,4,'r64647','khbdjbjd'));
        testPlan.wrpClassList = wrpLt1;
        testPlan.deleteProduct();
        Test.stopTest();
    }
    public static testMethod void callMeth2()
    {

        Test.StartTest();
        DuplicateProduct  testPlan = new DuplicateProduct();
        testPlan.noOfRow = 13;
        testPlan.addRow();
        List<DuplicateProduct.WrapperCls> wrpLt1 = new List<DuplicateProduct.WrapperCls>();
        wrpLt1.add(new DuplicateProduct.WrapperCls(1,1,'',''));
        testPlan.wrpClassList = wrpLt1;
        testPlan.deleteProduct();
        Test.stopTest();
    }
0

I found whats wrong here, There is a trigger which insert standard price book entry on product insert and I also insert standard price book entry in test class hence error : duplicate value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.