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
Joshua Anderson 44Joshua Anderson 44 

Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, The price book entry is in a different price book than the one assigned to the Quote, or Quote has no price book assigned. I cant Figure out what I'm doing wrong

@isTest
public class TestBookingReviewPageControllerSynced {
    @testSetup static void setup(){
        Id                  pbId        = Test.getStandardPricebookId();
        Pricebook2          stdPb       = new Pricebook2(Name = 'Standard Price Book', Id = pbId, isActive = true);
        upsert stdPb;

        System.debug('Standard pricebook list just inserted ' + stdPb);
        
        Map<String, Id>     rtMap       = new Map<String, Id>();
        List<RecordType>    rtList      = new List<RecordType>([SELECT Id, Name FROM RecordType]);
        for(RecordType rt : rtList){ rtMap.put(rt.Name, rt.Id); }
        
        List<Account>       aList       = new List<Account>();
        Account             newAccount  = new Account(Name = 'Test Account', Phone = '(555) 555-5555', Customer_Class__c = 'Defense Industry', Entity_Type__c = 'Domestic', Finalization_Status__c = 'Finalized', RecordTypeId = rtMap.get('US Federal Government Customer'));
        Account             newSupplier = new Account(Name = 'Test Supplier Account', Phone = '(555) 777-4242', Entity_Type__c = 'Domestic', Finalization_Status__c = 'Finalized', RecordTypeId = rtMap.get('Partner Supplier'));
        aList.add(newAccount);
        aList.add(newSupplier);
        insert aList;
        
        List<Address__c>    adList      = new List<Address__c>();
        Address__c newBillingAddress = new Address__c( 
            Account_Name__c                 = newAccount.Id, 
            Address_Line_1__c               = '536 Viking Dr', 
            Address_Line_2__c               = 'Building 1', 
            City__c                         = 'Virginia Beach', 
            State__c                        = 'Virginia', 
            State_Code__c                   = 'VA', 
            Postal_Code__c                  = '23452',  
            Country__c                      = 'United States', 
            Country_Code__c                 = 'US', 
            Type__c                         = 'Billing', 
            Primary__c                      = true
        );        
        
        Address__c newShippingAddress = new Address__c(
            Account_Name__c                 = newAccount.Id, 
            Address_Line_1__c               = '621 Lynnhaven Pkwy', 
            Address_Line_2__c               = 'Suite 160', 
            City__c                         = 'Virginia Beach', 
            State__c                        = 'Virginia', 
            State_Code__c                   = 'VA',
            Postal_Code__c                  = '23452',
            Country__c                      = 'United States', 
            Country_Code__c                 = 'US', 
            Type__c                         = 'Shipping'
        );
        adList.add(newBillingAddress);
        adList.add(newShippingAddress);
        insert adList;
        
        Contact             newContact  = new Contact(FirstName='Erik', LastName='Rodgers', AccountID = newAccount.Id, Phone='(555) 555-55555');
        insert newContact;
        
        List<Sales_Rep__c>  srList      = new List<Sales_Rep__c>();
        Sales_Rep__c        insideRep   = new Sales_Rep__c(Name = 'Inside Sales Rep Jane', Type__c = 'ISR', Active__c = true, Oracle_ID__c = '900237164');
        Sales_Rep__c        outsideRep  = new Sales_Rep__c(Name = 'Outside Sales Rep John', Type__c = 'OSR', Active__c = true, Oracle_ID__c = '900055045');
        srList.add(insideRep);
        srList.add(outsideRep);
        
        List<Product2>      productList = new List<Product2>();
        Product2 productA = new Product2( 
            Name                            = 'Item A',
            ProductCode                     = '78542-856-8547',
            Supplier_Product_Number__c      = '78542-856-8547',
            Supplier_Name__c                = newSupplier.Id,
            GSA_Price__c                    = 10.50,
            ADS_Cost__c                     = 8.50,
            IsActive                        = true,
            Product_Status__c               = 'Prospective',
            Category__c                     = 'TBD',
            Sub_Category__c                 = 'To Be Determined',
            Berry_Compliance_Status__c      = 'No',
            Country_of_Origin__c            = 'United States',
            Hazard_Class__c                 = 'EXP 2',
            Reporting_Category__c           = 'APPAREL',
            NSN__c                          = '4520-01-649-2495',
            Manufacturer_Product_Number__C  = '78542-856-8547'
        );
        productList.add(productA);              
        insert productList;
        System.debug('Product list just inserted ' + productList);

        List<PricebookEntry> entry = [ SELECT ID FROM PricebookEntry WHERE Pricebook2Id = :stdPb.id];
        System.debug('PricebookEntry list just inserted ' + entry);

        // Set<Id> pbEntryid = new Map<Id, PricebookEntry>([SELECT Id FROM PricebookEntry WHERE Pricebook2Id = :stdPb.id]).keySet();
        // System.debug('PricebookEntry list just inserted ' + pbEntryid);

        
        Opportunity         opp         = new Opportunity(Name = 'Test Opportunity', AccountId = newAccount.Id, CloseDate = date.Today(), StageName = 'Quoted', Pricebook2Id = stdPb.Id);
        insert opp;

        System.debug('Opportunity list just inserted ' + opp);
        
        Contract            newContract = new Contract(
            AccountId                       = newAccount.Id, 
            Name                            = 'Contract Name', 
            Status                          = 'Draft', 
            StartDate                       = date.Today(), 
            ADS_Contract_Number__c          = 'ADS Number', 
            Customer_Contract_Number__c     = 'Customer Number', 
            Quote_Enabled__c                = true, 
            Pricebook2                      = [SELECT Id FROM Pricebook2 WHERE Id= :stdPb.Id], 
            Participating_Customer_Class__c = 'Air Force;Army', 
            Berry_Compliance__c             = 'No'
        );
        insert newContract;
        
        Quote newQuote = new Quote(
            Name                      = 'Test Name',
            OpportunityId             = opp.Id,
            Opportunity2__c           = opp.Id, 
            Type__c                   = 'Quote', 
            Status__c                 = 'Draft', 
            Contract__c                     = newContract.Id, 
            Bill_To_Address__c              = newBillingAddress.Id, 
            Ship_To_Address__c              = newShippingAddress.Id, 
            Bill_To_Contact__c              = newContact.Id, 
            Ship_To_Contact__c              = newContact.Id, 
            Bill_To_Account__c              = newAccount.Id, 
            Ship_To_Account__c              = newAccount.Id,
            Primary__c                = true,
            Account__c                = newAccount.Id,
            PriceBook__c = stdPb.id

        );
        insert newQuote;

        System.debug('New Quote list just inserted ' + newQuote);

        QuoteLineItem newQuoteLineI = new QuoteLineItem( 
            QuoteId                   = newQuote.Id,
            // Product__c                = productA.Id,
            Deal_Strategy__c          = 'Some Deal Strategy',
            Quantity__c               = 175,
            Number__c                 = 1,
            GSA_Price__c              = 10.50,
            ADS_Cost__c               = 8.50,
            Quantity                  = 1,
            // UnitPrice                 = 11.50,
            // Product2Id                = productA.Id,
            PricebookEntryId          = '01ur0000000j1oiAAA',
            UnitCost__c               = 8.50
        );
        insert newQuoteLineI;

        System.debug('New Quote line item list just inserted ' + newQuoteLineI);

    }

    @istest static void testController(){
        Opportunity     opp             = [SELECT Id FROM Opportunity LIMIT 1];
        
        // Retrieve the standard controller for the opportunity
        ApexPages.standardController    opportunityController   = new ApexPages.standardController(opp);
        BookingReviewPageController     controller              = new BookingReviewPageController(opportunityController); 
        
        System.assert( controller.opportunityProductList.size() == 1 );
    }
}

 
Best Answer chosen by Joshua Anderson 44
AnudeepAnudeep (Salesforce Developers) 
Hi Joshua, 

This usually happens when the pricebookId is not set when creating a quote

Here a sample test method. Notice that quote has Pricebook2Id set. Also, see this example as a reference. Although the error message is slightly different, the underlying cause is the same
@isTest static void insertOpp() {
               
Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true );
    insert pb;
Product2 prod = new Product2(Name = 'SLA: Bronze', IsActive = true);
    insert prod;
PricebookEntry pbe=new PricebookEntry(unitprice=0.01,Product2Id=prod.Id, Pricebook2Id=Test.getStandardPricebookId(), IsActive= true); 
     insert pbe;      
           
        Account acc = new Account (name='Acme');
        insert acc;
        Opportunity opp= new Opportunity ();
        opp.name= 'Testopp';
        Opp.Accountid= acc.id;
        opp.CloseDate= date.today();
        opp.StageName= 'Qualification';
       opp.Pricebook2id=Test.getStandardPricebookId();
        
        insert opp;
        
OpportunityLineItem oppLine = new OpportunityLineItem( pricebookentryid=pbe.Id,TotalPrice=2000, Quantity = 2,Opportunityid = opp.Id);
insert oppLine;       
        
        Quote q= new Quote ();
        	 q.Name= 'Testq';
        	q.OpportunityId= Opp.id;
         	q.quotetoinvoice__C= TRUE;
         	q.REP__C= 'AC' ;
        	q.BillingStreet= '123';
        	q.BillingCity= 'City';
        	q.BillingPostalCode= '12345';
             q.Pricebook2Id= Test.getStandardPricebookId();
           
        	
        	insert q;
       
      QuoteLineItem qli= new QuoteLineItem(Quoteid=q.id, PricebookEntryid= pbe.Id,  quantity=2, unitprice=10000);
        insert qli;
        opp.StageName= 'Closed Won';

									}
    


If you find this information helpful, please mark this answer as Best. It may help others in the community. Thank You!

Anudeep

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Joshua, 

This usually happens when the pricebookId is not set when creating a quote

Here a sample test method. Notice that quote has Pricebook2Id set. Also, see this example as a reference. Although the error message is slightly different, the underlying cause is the same
@isTest static void insertOpp() {
               
Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true );
    insert pb;
Product2 prod = new Product2(Name = 'SLA: Bronze', IsActive = true);
    insert prod;
PricebookEntry pbe=new PricebookEntry(unitprice=0.01,Product2Id=prod.Id, Pricebook2Id=Test.getStandardPricebookId(), IsActive= true); 
     insert pbe;      
           
        Account acc = new Account (name='Acme');
        insert acc;
        Opportunity opp= new Opportunity ();
        opp.name= 'Testopp';
        Opp.Accountid= acc.id;
        opp.CloseDate= date.today();
        opp.StageName= 'Qualification';
       opp.Pricebook2id=Test.getStandardPricebookId();
        
        insert opp;
        
OpportunityLineItem oppLine = new OpportunityLineItem( pricebookentryid=pbe.Id,TotalPrice=2000, Quantity = 2,Opportunityid = opp.Id);
insert oppLine;       
        
        Quote q= new Quote ();
        	 q.Name= 'Testq';
        	q.OpportunityId= Opp.id;
         	q.quotetoinvoice__C= TRUE;
         	q.REP__C= 'AC' ;
        	q.BillingStreet= '123';
        	q.BillingCity= 'City';
        	q.BillingPostalCode= '12345';
             q.Pricebook2Id= Test.getStandardPricebookId();
           
        	
        	insert q;
       
      QuoteLineItem qli= new QuoteLineItem(Quoteid=q.id, PricebookEntryid= pbe.Id,  quantity=2, unitprice=10000);
        insert qli;
        opp.StageName= 'Closed Won';

									}
    


If you find this information helpful, please mark this answer as Best. It may help others in the community. Thank You!

Anudeep
This was selected as the best answer
Andrew GAndrew G
Hi Joshua

I suspect the issue is that at line 143 you are inserting the PBE with a hard coded Id.  I would suspect that does not match the PBE / PB combinations that you are setting up in your the rest of your test class.

the code supplied by Anudeep should resolve that.