1

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.

2 Answers 2

0

You inserted the quote before you created the pricebook you use later, and you did not assign it to the quote. Thus you cannot assign a PriceBookEntry to the QuoteLineItem.

0

You need to insert the pricebook prior to creating the quote. Then you need to set the Pricebook2Id of the quote before inserting it.

Quote quoteObj = new quote(Name='quote1');
quoteObj.OpportunityId=op.id;
quoteObj. Pricebook2Id = yourQuote.Id;   //Use your inserted quote here
Database.insert(quoteObj);

You can find this Pricebook2Id field in the documentation below

https://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_quote.htm

You must log in to answer this question.

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