You need to sign in to do that
Don't have an account?
Palmira Angelova
Test Coverage - List has no rows for assignment to SObjec
Hi friends,
I'm an experienced admin but still very new to development.
I'm trying to push some code that will mandate that a Contact Role be select before an Opportunity can be edited (this is necessary for an integration I'm working on with Hubspot). However, I'm running into the below error in my test case and can't figure out what I'm doing wrong - I've read about this on other forums, but selecting a price book is not like creating an object like an oppty or product in my eyes, so not sure how this applies:
"System.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.NumberofContactRoles_Test.TestCase1: line 26, column 1" (this is the PriceBook2 line below)
Could someone help me tweak the test case to avoid this error? Thank you sincerely for your help!
@isTest
public class NumberofContactRoles_Test{
public static testmethod void TestCase1()
{
Contact ConTest = new Contact();
ConTest.FirstName = 'TEstCon';
ConTest.LastName = 'LastName';
insert ConTest;
System.assertNotEquals(null,ConTest.Id);
Opportunity Opp1 = new Opportunity();
Opp1.Name = 'TestOpp';
Opp1.StageName = 'Prospect';
Opp1.CloseDate = System.today()+10;
insert Opp1;
System.assertNotEquals(null,Opp1.Id);
Product2 pd = new Product2(Name='Additional Processor Routing2',isActive=true, Family='PR', ProductCode='Payments & Security');
insert pd;
PriceBook2 pb2Standard = [select Id from Pricebook2 where isStandard=true limit 1];
Id standardPriceBookId = pb2Standard.Id;
PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=pd.Id, UnitPrice=99, isActive=true);
insert pbe;
OpportunityLineItem Oli1 = new OpportunityLineItem();
Oli1.OpportunityId = opp1.Id;
Oli1.PricebookEntryId = [select Id from PricebookEntry where isactive=true AND product2.Name='Additional Processor Routing' and Pricebook2.Isstandard=True limit 1][0].id;
Oli1.UnitPrice = 50;
Oli1.Quantity=10;
insert Oli1;
OpportunityContactRole oppRole = new OpportunityContactRole();
oppRole.OpportunityId = Opp1.Id;
oppRole.IsPrimary = True;
oppRole.ContactId = ConTest.Id;
insert oppRole;
System.assertNotEquals(null,OppRole.Id);
Opp1.Name = 'TestOpp2';
update Opp1;
}
}
I'm an experienced admin but still very new to development.
I'm trying to push some code that will mandate that a Contact Role be select before an Opportunity can be edited (this is necessary for an integration I'm working on with Hubspot). However, I'm running into the below error in my test case and can't figure out what I'm doing wrong - I've read about this on other forums, but selecting a price book is not like creating an object like an oppty or product in my eyes, so not sure how this applies:
"System.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.NumberofContactRoles_Test.TestCase1: line 26, column 1" (this is the PriceBook2 line below)
Could someone help me tweak the test case to avoid this error? Thank you sincerely for your help!
@isTest
public class NumberofContactRoles_Test{
public static testmethod void TestCase1()
{
Contact ConTest = new Contact();
ConTest.FirstName = 'TEstCon';
ConTest.LastName = 'LastName';
insert ConTest;
System.assertNotEquals(null,ConTest.Id);
Opportunity Opp1 = new Opportunity();
Opp1.Name = 'TestOpp';
Opp1.StageName = 'Prospect';
Opp1.CloseDate = System.today()+10;
insert Opp1;
System.assertNotEquals(null,Opp1.Id);
Product2 pd = new Product2(Name='Additional Processor Routing2',isActive=true, Family='PR', ProductCode='Payments & Security');
insert pd;
PriceBook2 pb2Standard = [select Id from Pricebook2 where isStandard=true limit 1];
Id standardPriceBookId = pb2Standard.Id;
PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=pd.Id, UnitPrice=99, isActive=true);
insert pbe;
OpportunityLineItem Oli1 = new OpportunityLineItem();
Oli1.OpportunityId = opp1.Id;
Oli1.PricebookEntryId = [select Id from PricebookEntry where isactive=true AND product2.Name='Additional Processor Routing' and Pricebook2.Isstandard=True limit 1][0].id;
Oli1.UnitPrice = 50;
Oli1.Quantity=10;
insert Oli1;
OpportunityContactRole oppRole = new OpportunityContactRole();
oppRole.OpportunityId = Opp1.Id;
oppRole.IsPrimary = True;
oppRole.ContactId = ConTest.Id;
insert oppRole;
System.assertNotEquals(null,OppRole.Id);
Opp1.Name = 'TestOpp2';
update Opp1;
}
}
Id pricebookId = Test.getStandardPricebookId();
Please update your code like below
Let us know if this will help you
All Answers
Id pricebookId = Test.getStandardPricebookId();
Please update your code like below
Let us know if this will help you