You need to sign in to do that
Don't have an account?

Test Class on Line Items - Field Integrity Error
I'm hoping someone can help. I'm writing a test class for a simple Line Item trigger (totally new at this). I am getting an error that the PriceBookEntryId can't be blank, but every time I try to pull in an ID, I get the error that my query returned no rows.
Original trigger:
1 2 3 4 | trigger OpportunityLineItemBeforeDelete on OpportunityLineItem (before delete) {
|
Test class that gives me the error: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId, unknown (versions 3.0 and higher must specify pricebook entry id; others must specify product id): [PricebookEntryId, unknown]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | @IsTest public class LIDeleteClass{ static TestMethod void InsertOppty() {Date myDate = date.newinstance(2014, 12, 17); Opportunity oppty = new Opportunity(Name='Test', StageName='IO Signed', CloseDate=myDate); insert oppty; Opportunity oppty1 = [select id, Name, Stagename, Closedate from Opportunity where Name = :'Test' LIMIT 1]; //PricebookEntry pbe = [select Id // from PricebookEntry // where Product2.Name = :'Amazon Ad Platform (AAP)' // and Pricebook2.Name = :'Standard Price Book' LIMIT 1]; OpportunityLineItem li = new OpportunityLineItem(OpportunityId=oppty1.id, Quantity=12000,TotalPrice=12000); insert li; Test.starttest(); try { delete li; System.assert(false); } catch (DMLException e) { } Test.stoptest(); }} |
When I uncomment the pbe variable, I get the error System.QueryException: List has no rows for assignment to SObject.
Any help would be appreciated.
hi,
you need to make sure that PricebookEntry record is not null and then add
PricebookEntryId = pbe.Id
to the OpportunityLineItem object.
also, a good pratice to follow when developing test methods is to make sure that you create all the data needed during the test - so that we don't rely on org's data....which can be realy an issue when deploying code to production.
Regards
Manuel
All Answers
hi,
you need to make sure that PricebookEntry record is not null and then add
PricebookEntryId = pbe.Id
to the OpportunityLineItem object.
also, a good pratice to follow when developing test methods is to make sure that you create all the data needed during the test - so that we don't rely on org's data....which can be realy an issue when deploying code to production.
Regards
Manuel
Manuel,
Thanks. I thought so too, but am I able to write to the pricebook? If so, what would that look like? I was under the impression that it couldn't be written to from the class.
Shane
Sure you are, see an example that I grabbed from one of my classes:
...
Opportunity[] ops = new Opportunity[]{ o1,o2,o3};
insert ops;
Product2 p = new Product2(name='x', Description = 'desc',Product_Type__c = 'bla bla', Product_Class__c ='test');
insert p;
Pricebook2 stdPb = [select Id from Pricebook2 where isStandard=true limit 1];
PricebookEntry pbe = new PricebookEntry(pricebook2id = stdPb.id, product2id = p.id, unitprice=1.0, isActive=true);
insert pbe;
OpportunityLineItem ol1 = new OpportunityLineItem (OpportunityId = o1.Id, PricebookEntryId = pbe.Id, Quantity = 1, UnitPrice = 1;
....
Note the line for the Pricebook2
Regards
Manuel
Manuel,
Thanks again. I made similar updates, but I was still getting a no rows to assign error on the Select ID from Pricebook2, even though the pricebook has products populated. I was able to assign the PricebookID manually and got 100% code coverage. I may have to make similar adjustments in prod.
Regards!
Shane
yeah this test class gets 100% but I'm not sure if it will work in prod
Thanks, Ross. I'm going to try this approach, too, so I can see for myself how they both work.