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
Gaurav AgnihotriGaurav Agnihotri 

Test Class fails FIELD_INTEGRITY_EXCEPTION

I created a test class and getting an error message:
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, The pricebook entry is inactive.: [PricebookEntryId]

Below is the class
public string QuoteRefresh1(string sQuoteId){
       String sQuoteAccountId=[SELECT Pelco_Account_Name__c FROM Quote where Id=:sQuoteId].Pelco_Account_Name__c;
       system.debug('sQuoteAccountId='+sQuoteAccountId); 
       String CustomerNumber=[SELECT Customer_Number__c FROM Account WHERE Id =:sQuoteAccountId].Customer_Number__c;
       system.debug('CustomerNumber='+CustomerNumber); 
       for(QuoteLineItem pi:[SELECT CreatedById,CreatedDate,Description,Id,IsDeleted,LastModifiedById,LastModifiedDate,LineNumber,ListPrice,PricebookEntryId,Product2Id,Product2.Item__c,Quantity,QuoteId,SortOrder,Subtotal,SystemModstamp,TotalPrice,UnitPrice,Currency__c,Dealer_Price__c,Regular_Price__c FROM QuoteLineItem  where QuoteId =:sQuoteId])
       {
           string sProduct2Id= pi.Product2Id;
           String ItemNumber= pi.Product2.Item__c ;
           system.debug('ItemNumber='+ItemNumber);
            //calculating prices, discounts and currency
           CalculatePelcoDiscount1 NewDiscount= new CalculatePelcoDiscount1();
           double PelcoDiscount=NewDiscount.CalculatePelcoDiscount1(ItemNumber,CustomerNumber );
           CalculatePelcoPrice newPrice=new CalculatePelcoPrice();
           double pelcoPrice=newPrice.CalculatePelcoPrice(ItemNumber,CustomerNumber );
           string pelcoCurrency=newPrice.CalculatePelcoCurrency(CustomerNumber);
           pi.Currency__c=pelcoCurrency;
           system.debug('pelcoCurrency='+pelcoCurrency);
           pi.Dealer_Price__c=pelcoPrice;
           system.debug('pelcoPrice='+pelcoPrice);
           pi.Discount=PelcoDiscount;
           system.debug('PelcoDiscount='+PelcoDiscount);
           //pi.Discounted_Price__c=100;
           //adding records to the list
           items.add(pi);
       }
       if(items.size() > 0 )
        {
            update items;
        }
           return ('Success');  
   }
  }//end class

and test class
@istest
private class TestQuoteRefresh1 {
    static testMethod void testQR1() {
//create Price book
        /*Pricebook2 pb=new Pricebook2();
        pb.IsActive=true;
        pb.Name='test';
        insert pb;*/
        //Create Account
        Account a = new Account();
    	a.Name = 'Ritzy';
    	insert a;
        Account b=new Account();
        b.Name='Testb';
        insert b;
        
        //Create Opportunity
        Opportunity opty=new Opportunity();
		opty.Name='Test Opportunity';
        opty.StageName='Prospecting';
        opty.CloseDate=date.valueof('2015-09-10');
        opty.AccountId=a.Id;
        //opty.Pelco_Account_Name__c=PA.id;
		Insert opty; 
        
        //Create pelco account
        Pelco_Account__c PA =new Pelco_Account__c();
        PA.Name='Ritzy';
        //PA.OpportunityId=Opty.id; 
        PA.AccountName__c=a.Id;
        PA.OpportunityName__c=Opty.id;
        Insert PA;
         Id priceBookId = Test.getStandardPricebookId();
         //Insert Quote
        Quote q =new Quote();
         q.name='Test Quote';
         q.opportunityId=opty.id;
         q.Pelco_Account_Name__c=b.id;
         q.Pricebook2Id=priceBookId;
        insert q; 
       
        //insert Quote Line Item
        Product2 prod = new Product2(
			 Name = 'ENGEH16-2P',
			 ProductCode = 'ENGEH16-2P',
			 Item__c = 'ENGEH16-2P',
             Discount_Code__c='A',
			 isActive = true,
            Dealer_Price__c=100 
		);
		insert prod;
        //create price book entry
        PricebookEntry PBE= new PricebookEntry();
        //PBE.Name='ENGEH16-2P';
        PBE.Pricebook2Id=priceBookId;
        PBE.Product2Id=prod.id;
        PBE.UnitPrice=100;
        Insert PBE;
        //Create Quote Item 1
        QuoteLineItem QLI= new QuoteLineItem();
        QLI.Currency__c='USD';
        QLI.Dealer_Price__c=100;
        QLI.Discount=20;
        //QLI.Discounted_Price__c=70;
        //QLI.Incremental_Discount__c=10;
        //QLI.ListPrice=200;
        QLI.PricebookEntryId=PBE.Id;
        QLI.Product2Id=prod.id;
        QLI.Quantity=1;
        QLI.QuoteId=q.Id;
        //QLI.TotalPrice=100;
        QLI.UnitPrice=100;
        Insert QLI;
     QuoteRefresh1 QRefresh=new QuoteRefresh1();
     QRefresh.QuoteRefresh1(q.Id);
    }
}

You can see from the print screen that price book entry is active
User-added image
Best Answer chosen by Gaurav Agnihotri
Prashant WayalPrashant Wayal
Hi Gaurav,

Can you try below code;
 
PricebookEntry PBE= new PricebookEntry();
        //PBE.Name='ENGEH16-2P';
        //PBE.Pricebook2Id=pb.Id;
        PBE.Pricebook2Id=priceBookId2;
        PBE.Product2Id=prod.id;
        PBE.UnitPrice=100;
        PBE.UseStandardPrice=false;
        PBE.isActive = true;
        Insert PBE;

Hope it helps you.

Thanks,
Prashant

All Answers

Gaurav AgnihotriGaurav Agnihotri
This is what I am not able to understand.
I am extracting Price book entry Id using 
Id priceBookId = Test.getStandardPricebookId();

When i a trying to create a Quote Line Item
QuoteLineItem QLI= new QuoteLineItem();
        QLI.Currency__c='USD';
        QLI.Dealer_Price__c=100;
        QLI.Discount=20;
        QLI.PricebookEntryId=PBE.id;
        QLI.Product2Id=prod.id;
        QLI.Quantity=1;
        QLI.QuoteId=q.Id;
        QLI.UnitPrice=100;
        Insert QLI;
I am creating Price book entry 
PricebookEntry PBE= new PricebookEntry();
        //PBE.Name='ENGEH16-2P';
        //PBE.Pricebook2Id=pb.Id;
        PBE.Pricebook2Id=priceBookId2;
        PBE.Product2Id=prod.id;
        PBE.UnitPrice=100;
        PBE.UseStandardPrice=false;
        Insert PBE;
why would I get this error message:
16:57:51:792 FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, The pricebook entry is inactive.: [PricebookEntryId]



 
Gaurav AgnihotriGaurav Agnihotri
Anyone?
Prashant WayalPrashant Wayal
Hi Gaurav,

Can you try below code;
 
PricebookEntry PBE= new PricebookEntry();
        //PBE.Name='ENGEH16-2P';
        //PBE.Pricebook2Id=pb.Id;
        PBE.Pricebook2Id=priceBookId2;
        PBE.Product2Id=prod.id;
        PBE.UnitPrice=100;
        PBE.UseStandardPrice=false;
        PBE.isActive = true;
        Insert PBE;

Hope it helps you.

Thanks,
Prashant
This was selected as the best answer
Gaurav AgnihotriGaurav Agnihotri
Hi Prashant, 

Thanks.That did resolve the issue.
I am calling several other classes in that class. Their code coverage is not so good. However, they had full code coverage before . what should I do now?
Gaurav AgnihotriGaurav Agnihotri
The way out is to run all the test for code coverage  together.