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
LuvaraLuvara 

Test Class Issue for Quote Pricebook

I've seen this issue many times on the forums and tried the suggested solutions, but I am still having trouble figuring this one out.

 

I am getting the error

 

"System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, The pricebook entry is in a different pricebook than the one assigned to the Invoice, or Invoice has no pricebook assigned.: [PricebookEntryId]"

 

(Invoice is actually Quote, the label is renamed in the org)

 

Here is my trigger:

 

trigger trgQuoteItems on Quote (after insert) {

    /* 
    This trigger adds a processing fee line item to a Quote when the Opportunity's bank meets a certain criteria. 
    Other banks can be added using the Processing Fee field on the quote.
    */ 
    
    //Check how many Processing Fees there are
    Integer i2 = [select count() from PricebookEntry where Name LIKE '%Processing Fee%' and isactive=true limit 1 ];
    System.Debug('### size fee items '+ i2); 
   
    For (Quote q : Trigger.new)
    
    //Prevent recursion and prevent the trigger from running if there are no processing fee line items  
    if(Constants.runTrigger && i2 !=0) {
    
    //Get the pricebook of the quote
    ID QPB1 = [select Pricebook2Id from Quote limit 1].Pricebook2Id ;
    
    //Find the fee item in the Quotes pricebook
    PricebookEntry P = [select Id from PricebookEntry where Pricebook2Id=:QPB1 and Name LIKE '%Processing Fee%' and isactive=true limit 1 ];

    //See if there are any Processing Fee Line items already
    List<QuoteLineItem> FeeLineItems = new List<QuoteLineItem>();
    Integer i = [Select count() From QuoteLineItem Where QuoteId =: q.id and PricebookEntryId = :p.Id];
    System.Debug('### size of line items '+ i);

    List<QuoteLineItem> listofQLI = new List<QuoteLineItem>();

    if (q.Processing_Fee__c==1 && i ==0)
    {
    QuoteLineItem QLI = new QuoteLineItem (
    QuoteId=q.id,
    UnitPrice=0,
    Quantity =1,
    PricebookEntryId=P.Id
    );
    listofQLI.add(QLI);
    }
    //Set static variable to false
    Constants.runTrigger = false;
    Insert listofQLI;
    }

}

 Here is the test class that is failing. The odd thing is that the test class doesn't fail all the time in the sandbox. There's random times when it won't fail.

 

@isTest (seeAllData=true) 
public class NESFTests {

		//Test the trgQuoteItems Trigger
		static testMethod void TesttrgQuoteItems() {
		 
		Test.startTest();
		//Add some products
		Product2 prd1 = new Product2 (Name='Product1',Description='Test Product Entry 1',productCode = 'PF-05', isActive = true, Family='Fee');
		insert prd1;
		Product2 prd2 = new Product2 (Name='Processing Fee', Description='my fee',productCode = 'PF-01',isActive=true, Family='Fee');
		insert prd2;

		//get standard pricebook
		//Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
		Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where Name = 'Standard Price Book'];
		if (!standardPb.isActive) {
            standardPb.isActive = true;
            update standardPb;
		}
	
		Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook',Description='Test Pricebook', isActive=true);
		insert pbk1;
		
		//Add those products to a pricebook
		PricebookEntry pbe1 = new PricebookEntry (Product2ID=prd1.id,Pricebook2ID=standardPb.id,UnitPrice=0.0, isActive=true , UseStandardPrice = false);
		insert pbe1;
		
		PricebookEntry pbe2 = new PricebookEntry (Product2ID=prd2.id,Pricebook2ID=standardPb.id,UnitPrice=0.0, isActive=true,  UseStandardPrice = false);
		insert pbe2;
		
		//Create an Opp with the standard pricebook
		Opportunity qOpp = new Opportunity(AccountId=accounts[0].Id, Name='Renewal - New - Addon', bps__c=1000,
	                ForecastCategoryName='Commit',LeadSource='Source',
	                StageName='20 - Qualified Opportunity', NextStep='notes', Type='New Business', Depository__c ='Bank United',
	                Pricebook2Id=standardPb.Id,
	                CloseDate=System.today()
	                );
	    insert qOpp;
		
		//Insert opp line items into our opp
		OpportunityLineItem lineItem1 = new OpportunityLineItem (OpportunityID=qOpp.id,PriceBookEntryID=pbe1.id, quantity=1, totalprice=0.0);
		insert lineItem1;
		
		//OpportunityLineItem lineItem2 = new OpportunityLineItem (OpportunityID=qOpp.id,PriceBookEntryID=pbe2.id, quantity=1, totalprice=0.0);
		//insert lineItem2;
		

		//Insert a quote
		Quote qte1 = new Quote (OpportunityId=qOpp.id, name='my quote', Pricebook2Id=standardPb.Id,
		Bill_To_City__c='test',Bill_To_Company__c='company',Bill_To_Country__c='USA',
		Bill_To_State__c='CA',Bill_To_Street__c='Street',Bill_To__c='name',Bill_To_Zip__c='95113');
		insert qte1;
			
		Test.stopTest();
		}


/* Setup Test Data */

	 private static list<Account> accounts {
	        get {
	            if (accounts == null) {
	                accounts = new List<Account> {
	                        new Account(name= 'Regular Account',Phone='(123)456-7890', type='1031 prospect',
	                        Industry='mining',Website='www.nesf.com')  
	                };
	
	                insert accounts;
	            }
	            return accounts;
	        }
	        private set;
	    }
	
	
	    private static list<Contact> contacts {
	        get {
	            if (contacts == null) {
	                contacts = new list<Contact> {
	                    new Contact(FirstName = 'Test1', LastName = 'Contact1',Phone='408-555-1234', AccountId = accounts[0].Id, Email = 'test@nesf.com')
	                };
	                insert contacts;
	            }
	            return contacts;
	        }
	        private set;
	    }
	
	    private static Opportunity opp {
	        get {
	            if (opp == null) {
	                opp = new Opportunity(AccountId=accounts[0].Id, Name='Renewal - New - Addon', bps__c=1000,
	                ForecastCategoryName='Commit',LeadSource='Source',
	                StageName='20 - Qualified Opportunity', NextStep='notes', Type='New Business', Depository__c ='Bank United',
	                CloseDate=System.today()
	                );
	                insert opp;             
	            }
	            return opp;
	        }
	        private set;
	    }
	    
}

 

Any help would be greatly appreciated.. Thanks!

 

Chris