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 Quote, or Quote has no pricebook assigned.: [PricebookEntryId]
I have inserted objects like this:
Quote quoteObj = new quote(Name='quote1');
//quoteObj.Name='quote1';
//quoteObj.='';
quoteObj.OpportunityId=op.id;
Database.insert(quoteObj);
Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1',productCode = 'ABC', isActive = true);
Database.insert(prd1);
Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
Database.insert(pbk1);
PricebookEntry pbe1 = new PricebookEntry (Product2ID= prd1.id,Pricebook2ID=standardPb.id,UnitPrice=50,IsActive = true);
PricebookEntry customprice = new pricebookentry( product2id = prd1.id, pricebook2id = pbk1.id,UnitPrice=50, usestandardprice = false, IsActive = true);
Database.insert(pbe1);
Database.insert(customprice);
After that, I started like this: i can't get where i am wrong.
QuoteLineItem qli= new QuoteLineItem();
qli.UnitPrice=100;
qli.Quantity=10;
qli.QuoteId=quoteObj.Id;
qli.Product2Id=prd1.Id;
qli.PricebookEntryId=customprice.Id;
Database.insert(qli);
isActive and usestandard fields are already checked. Please help.