You need to sign in to do that
Don't have an account?
Phil Westerby-Jones
Quote and Quote Line item Price Book mismatch Problems in Apex Test Class, only in Production instance
I'm getting this error "LB_TestProductExplosion.TestOpportunityWithTriggerProduct(), Details: 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] " when running my tests in the Operational Instance.
It works fine in my Dev and in The Sandbox of The Operational Instance.
I've burned over 6 hours on this now and have tried various changes/rewrites in my Apex code, which have basically made no difference. I've used Debug to display the Product, PB and PBE details step by step and all is fine in Dev/Sandbox, but on doing or testing the deployment in The Operational Environment, it's no go. I've searched the forums and this seems to be a known problem, but other than the real basic stuff answers, there's no real solution, which is unusual for SF problems in my experience.
VERY frustrating!! HELP!!!!
It works fine in my Dev and in The Sandbox of The Operational Instance.
I've burned over 6 hours on this now and have tried various changes/rewrites in my Apex code, which have basically made no difference. I've used Debug to display the Product, PB and PBE details step by step and all is fine in Dev/Sandbox, but on doing or testing the deployment in The Operational Environment, it's no go. I've searched the forums and this seems to be a known problem, but other than the real basic stuff answers, there's no real solution, which is unusual for SF problems in my experience.
VERY frustrating!! HELP!!!!
The diagnosois was significantly hampered until I finally found out that if I opened the Developer Console in Production BEFORE running the Validate/Deploy, I'd get the usual debug logs.......... so I was working in the dark before and just on SandBox logs #NewbieError
I've had to use (SeeAllData=true) from day one on all my tests which include Price Books, as I've never been able to set up a standard Price Book via Apex and therefore had to use the System Standard Price Book. How DO I setup a Standard Price Book in Apex by the way? :) Anyway this also meant that the other system data was visible......... On checking the Production Log, the Quote PBID was Different to the Opp PBID and on closer investigation....... guess what, there was already an existing Quote called "Test Quote"! Renamed it to "Test Quote 1" and all passed!!
Problem solved and a NUMBER of new things learned which hopefully others will learn from! Thanks for your help Venkat!!
All Answers
In your test method, make sure you assign the pricebook2Id field a value. Normally, when you create a Quote from the UI, Quote get the price book id from Opportunity. But in a Test environment, you need to set it.
Venkat
You may want post your test case code here so that we can see what is going on, if that is alright with you.
Thanks,
Venkat
As I've said, this works 100% in dev and sandbox, but not in Production!! I've checked the objects in Production and there's nothing obviously different and no more mandatory fields etc?
private static OpportunityLineItem createLineItem(String prodName) {
//Get the Opportunity
Opportunity o = [ SELECT ID, Pricebook2ID, Name
FROM Opportunity
WHERE Name=:prodName
LIMIT 1
];
system.debug('ProdOpp Name/o.ID:' + o.Name + '/' + o.Id);
system.debug('PriceBook2ID:' + o.Pricebook2ID);
//Get the Product
Product2 prod1 = [ SELECT id
FROM Product2
WHERE Name=:prodName
LIMIT 1
];
//Get Price Book Entry ID
PricebookEntry getpb = [ SELECT id
FROM PricebookEntry
WHERE Product2ID=:prod1.ID
LIMIT 1
];
OpportunityLineItem line = new OpportunityLineItem(
OpportunityID = o.ID,
PricebookEntryID = getpb.id,
UnitPrice = 123.45,
Quantity = 3);
Insert line;
//Get the Opportunity again
Opportunity o1 = [ SELECT ID, Pricebook2ID, Name
FROM Opportunity
WHERE Name=:prodName
LIMIT 1
];
system.debug('ProdOpp Name/o1.ID:' + o1.Name + '/' + o1.Id);
system.debug('PriceBook2ID:' + o1.Pricebook2ID);
Quote q = new Quote(
OpportunityID = o1.Id,
Name = 'Test Quote',
PriceBook2ID = o1.Pricebook2ID);
Insert q;
system.debug('Quote Q:' + q);
//Get the Quote
Quote thisQuote = [ SELECT id, PriceBook2ID
FROM Quote
WHERE Name='Test Quote'
LIMIT 1
];
system.debug('Quote thisQuote:' + thisQuote);
QuoteLineItem qLine = new QuoteLineItem(
QuoteID = thisQuote.id,
PricebookEntryID = getpb.id,
UnitPrice = 123.45,
Quantity = 3);
Insert qLine;
system.debug('QuoteLI qLine:' + qLine);
Return line;
}
I do not see any issues with the code. One thing though, before you call this method, Opportunity is already created. Make sure that you are assigning Pricebook when you create your Opportunity.
Venkat
I'm not specifically assigning the Price Book ID to the opportunity, but I'll try that and let you know.
Thank you for your help and feedback man, much appreciated!! P
I've put in even more debugs and the PB ID and PBE IDs match up, from Opp to Quote and both sets of line items, but SOMETHING isn't right when going to Production :( I'm really happy to blame myself, but this is looking like something wrong with the platform? - How can it pass in Sandbox, but not in Production?!!
This is the calling class:
and here is the factory class:
Good luck!! :)
The diagnosois was significantly hampered until I finally found out that if I opened the Developer Console in Production BEFORE running the Validate/Deploy, I'd get the usual debug logs.......... so I was working in the dark before and just on SandBox logs #NewbieError
I've had to use (SeeAllData=true) from day one on all my tests which include Price Books, as I've never been able to set up a standard Price Book via Apex and therefore had to use the System Standard Price Book. How DO I setup a Standard Price Book in Apex by the way? :) Anyway this also meant that the other system data was visible......... On checking the Production Log, the Quote PBID was Different to the Opp PBID and on closer investigation....... guess what, there was already an existing Quote called "Test Quote"! Renamed it to "Test Quote 1" and all passed!!
Problem solved and a NUMBER of new things learned which hopefully others will learn from! Thanks for your help Venkat!!