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.: Price Book Entry ID:

This is my below code

trigger test on Opportunity(After update)
   List<Id> lstId =new List<Id>(); 
   map<id,OpportunityLineItem> mapline= new  map<id,OpportunityLineItem>(); 

        for(Opportunity opp:Trigger.New)

        List<Opportunity> lstopp = [SELECT Id,Name,AccountId,Account.Name,account.billingCity,account.billingStreet,(select id,Product2id,PricebookEntryId,OpportunityId  from OpportunityLineItems)account.billingCity,account.billingState,account.billingCountry,Amount,StageName,CloseDate FROM Opportunity WHERE Id IN:lstId];

        List<Quote> quotlst=new List<Quote>();

        for(Opportunity  opp:lstopp)

         for(OpportunityLineItem opline:opp.OpportunityLineItems)


            Quote q = new Quote();
            q.name = opp.name;
            q.opportunityId = opp.id;
            q.billingStreet = opp.account.billingStreet;
            q.billingCity = opp.account.billingCity;
            q.billingState = opp.account.billingState;
            q.billingCountry = opp.account.billingCountry;
            q.BillingName = opp.Account.Name;


        if(!quotlst.isEmpty() && quotlst.size()>0)
            insert quotlst;

        List<Quote> quotetest = [SELECT Id,opportunityId  FROM Quote WHERE ID IN:quotlst];

            for(Quote que:quotetest)

            OpportunityLineItem oplitem=mapline.get(que.opportunityId);

            QuoteLineItem test = new QuoteLineItem(QuoteId = que.id , Quantity = 3.00 ,UnitPrice = 12,Product2id=oplitem.Product2id,PricebookEntryId = oplitem.PricebookEntryId ); //, PricebookEntryId = oplitem.Product2id);

             insert test ;


please anybody help me

2 Answers 2


In the part of the code where you're creating the quote, you need to assign the Pricebook2Id field.

q.Pricebook2Id = opp.Pricebook2Id;

I'm not familiar with these objects. But relating your error message to the Product and Schedule Objects ERD, it appears you need to specify a PriceBook2Id on your Quote (probably taken from the Opportunity object):

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .