You need to sign in to do that
Don't have an account?
System.QueryException: List has no rows for assignment to SObject
I'm creating a test method, but whenI try to select from the database I get "System.QueryException: List has no rows for assignment to SObject"
Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];
I've run the query in the force.com IDE and it works, I get the row that I expect. I don't know why its returning 0 rows when the test run. Any ideas?
Thanks in advance!
(FYI - I'm new to apex, this is only my second trigger/test method)
Here's the code:
@isTest
private class Test_Opp_Create_Contract_Entitlement {
static testMethod void myUnitTest() {
//create our test account
Account a = new Account();
a.name = 'test';
a.type = 'A+';
a.industry = 'Other';
a.billingcity = 'd';
a.billingcountry = 'USA';
a.billingState = 'co';
a.billingstreet = 'unknown';
a.billingpostalcode = '80204';
a.phone = '3039291726';
insert a;
//Create an opportunity
Opportunity o = new Opportunity();
o.Name = 'test';
o.AccountId = a.id;
o.Type = 'Infor (OEM)';
o.CloseDate = date.today();
o.StageName = 'Qualified';
o.LeadSource = 'other';
insert o;
Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];
Product2 p = new Product2();
p.Cost__c = 10;
p.Name = 'testp';
p.ProductCode = '12345';
p.Family = 'Software Support';
p.Create_Service_Contract_when_Sold__c = true;
insert p;
PricebookEntry pbe = new PricebookEntry();
pbe.Product2Id = p.id;
pbe.UnitPrice = 100;
pbe.Pricebook2Id = standardPB.Id;
pbe.IsActive = true;
insert pbe;
test.startTest();
//add line item
OpportunityLineItem oli = new OpportunityLineItem();
oli.OpportunityId = o.id;
oli.PricebookEntryId = pbe.Id;
oli.UnitPrice = pbe.UnitPrice;
oli.Quantity = 10;
insert oli;
//update status
o.StageName = 'Closed Won';
update o;
test.stopTest();
}
}
By default, tests are isolated from your production data. I can't recall which org settings are available,but I don't think PriceBooks are included by default. You can either create a Pricebook2 in your test code, or use the @isTest(SeeAllData=true) annotation (the preferred way is to create your PriceBook2 in your test code)
JeffM
All Answers
By default, tests are isolated from your production data. I can't recall which org settings are available,but I don't think PriceBooks are included by default. You can either create a Pricebook2 in your test code, or use the @isTest(SeeAllData=true) annotation (the preferred way is to create your PriceBook2 in your test code)
JeffM
Thanks Jeff!
@isTest(SeeAllData=true) worked.
I tried creating a pricebook a few times yesterday, but couldn't get a standard pricebook created and couldn't add the product to the "test" pricebook without a standard price. It was driving me crazy. Thanks for your help.