function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kim Lebicz 2Kim Lebicz 2 

List has no rows for assignment to sobject in Test Class with 2 tests

I have created a test class that runs 2 tests. The first test is successful but the second test fails. It give me this error,  "System.QueryException: List has no rows for assignment to SObject" and references this line as the problem, "RecordType ort=[select Id from RecordType where SobjectType = 'Opportunity' and DeveloperName = 'SB_Process'];". I'm confused because both tests are the same so why would it work in one test but not the other?
I'm very new to this so any assistance is greatly appreciated.
 
Raj VakatiRaj Vakati
Are you using System.runAs ? can you share the test classes ?
Kim Lebicz 2Kim Lebicz 2
Hi Raj. Thanks for looking at this.
Error Message - System.QueryException: List has no rows for assignment to SObject
Stack Trace - Class.ServiceFastQuoteControllerTest.testFastQuote2: line 58, column 1

@isTest
public class ServiceFastQuoteControllerTest {
     
    static testMethod void testFastQuote1(){
        Account a = new Account();
        a.name = 'Test';
        a.Street_Address_1__c = 'test';
        a.City__c = 'test';
        a.State_Providence__c = 'GA';
        a.Zip_Postal_Code__c = '30240';
        a.Cardiology_Account_Manager__c = UserInfo.getUserId();
        insert a;
        
        RecordType ort=[select Id from RecordType where SobjectType = 'Opportunity' and DeveloperName = 'CPQ_Service'];
        
        Opportunity opp = new Opportunity();
        opp.Name = 'Test';
        opp.CloseDate = system.today();
        opp.AccountId = a.Id;
        opp.RecordTypeId = ort.Id;
        opp.StageName = 'Qualification in Process';
        insert opp;
/*        
        Test.startTest();
        //PageReference pageRef = Page.FastQuoteOpp;
        //Test.setCurrentPage(pageRef);
        ApexPages.StandardController controller = new ApexPages.StandardController(opp);
        FastQuoteController controllerext = new FastQuoteController(controller);
        //PageReference pageRef = controllerext.OpenPage();
   //     System.assertEquals(controllerext.getFinishLocation().getUrl(), '/home/home.jsp');
   //     String curId = controllerext.getQuoteID();
   //     PageReference varQuoteId = controllerext.OpenPage();
            
        Test.stopTest();*/
        
                
        Test.StartTest(); 

            PageReference pageRef = Page.FastQuoteOpp; 
            pageRef.getParameters().put('opportunityId', String.valueOf(opp.Id));
            Test.setCurrentPage(pageRef);

            ServiceFastQuoteController testFQ = new ServiceFastQuoteController(new ApexPages.StandardController(opp));
            testFQ.OpenPage();  
        Test.StopTest();
    }
    
        static testMethod void testFastQuote2(){
        Account a = new Account();
        a.name = 'Test';
        a.Street_Address_1__c = 'test';
        a.City__c = 'test';
        a.State_Providence__c = 'GA';
        a.Zip_Postal_Code__c = '30240';
        a.Cardiology_Account_Manager__c = UserInfo.getUserId();
        insert a;
        
        RecordType ort=[select Id from RecordType where SobjectType = 'Opportunity' and DeveloperName = 'CPQ_Service'];
        
        Opportunity opp = new Opportunity();
        opp.Name = 'Test';
        opp.CloseDate = system.today();
        opp.AccountId = a.Id;
        opp.RecordTypeId = ort.Id;
        opp.Quote_Expires_On__c = system.today();
        opp.StageName = 'Qualification in Process';
        insert opp;
/*        
        Test.startTest();
        //PageReference pageRef = Page.FastQuoteOpp;
        //Test.setCurrentPage(pageRef);
        ApexPages.StandardController controller = new ApexPages.StandardController(opp);
        FastQuoteController controllerext = new FastQuoteController(controller);
        //PageReference pageRef = controllerext.OpenPage();
   //     System.assertEquals(controllerext.getFinishLocation().getUrl(), '/home/home.jsp');
   //     String curId = controllerext.getQuoteID();
   //     PageReference varQuoteId = controllerext.OpenPage();
            
        Test.stopTest();*/
        
                
        Test.StartTest(); 

            PageReference pageRef = Page.FastQuoteOpp; 
            pageRef.getParameters().put('opportunityId', String.valueOf(opp.Id));
            Test.setCurrentPage(pageRef);

            ServiceFastQuoteController testFQ = new ServiceFastQuoteController(new ApexPages.StandardController(opp));
            testFQ.OpenPage();  
        Test.StopTest();
    }
}
Raj VakatiRaj Vakati
Can you go and developer console and run this query to see the result

 
select Id from RecordType where SobjectType = 'Opportunity' and DeveloperName = 'CPQ_Service'

ANd try this too
select Id from RecordType where SobjectType = 'Opportunity'

​​​​​​​
Kim Lebicz 2Kim Lebicz 2
The first query returned the Opportunity recordtype id for CPQ_Service recordtype.
User-added image
The second query returned all of the Opportunity recordtype ids
User-added image