+ Start a Discussion
Palmira AngelovaPalmira 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;
    }
}
Best Answer chosen by Palmira Angelova
Amit Chaudhary 8Amit Chaudhary 8
Use below line to get price book

Id pricebookId = Test.getStandardPricebookId();

Please update your code like below
 
@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;
	*/
	
	Id pricebookId = Test.getStandardPricebookId();
	
    PricebookEntry pbe = new PricebookEntry(Pricebook2Id=pricebookId, Product2Id=pd.Id, UnitPrice=99, isActive=true);
    insert pbe;
    
    OpportunityLineItem Oli1 = new OpportunityLineItem();
    Oli1.OpportunityId = opp1.Id;
    Oli1.PricebookEntryId = pbe.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;
    }
}

Let us know if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Use below line to get price book

Id pricebookId = Test.getStandardPricebookId();

Please update your code like below
 
@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;
	*/
	
	Id pricebookId = Test.getStandardPricebookId();
	
    PricebookEntry pbe = new PricebookEntry(Pricebook2Id=pricebookId, Product2Id=pd.Id, UnitPrice=99, isActive=true);
    insert pbe;
    
    OpportunityLineItem Oli1 = new OpportunityLineItem();
    Oli1.OpportunityId = opp1.Id;
    Oli1.PricebookEntryId = pbe.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;
    }
}

Let us know if this will help you
 
This was selected as the best answer
Palmira AngelovaPalmira Angelova
Thank you, this is exactly what I needed!