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
Renee NicholusRenee Nicholus 

List has no rows for assignment to SObject error on validate

Getting the subject line error when validating my test class. I'm not a developer (someone from SF CS team that no longer works there helped me write this initially) so I think this is a simple fix, but I couldn't figure out how to update it based on the examples I saw.  Any help would be appreciated. Thanks!


@isTest
private class BusinessHoursTest {
    
    public static testMethod void BusinessHours(){        
        //Delcare var
        Decimal diff;
        
        //Test QFH data
        Quote_Field_History__c QFH = new Quote_Field_History__c();
        QFH.Quote__c = 'a2Lm00000007mTYEAY';
        QFH.Start_Time__c = system.now();
        QFH.Stop_Time__c = (system.now()+5);
        insert QFH;               
        
        
        system.debug('Inserting QFH with vaild QFH.Stop_Time__c value');
        
        BusinessHours BusinessHoursId = [SELECT Id FROM BusinessHours WHERE IsActive=true AND name= 'CS];
        QFH = [select ID, Stop_Time__c, Name from Quote_Field_History__c where Id = 'a2Lm00000007mTYEAY'];
        system.assert(QFH.Stop_Time__c != Null);
    }
}
Andrew GAndrew G
a SELECT statement will return a list, so i suspect you error is here
QFH = [select ID, Stop_Time__c, Name from Quote_Field_History__c where Id = 'a2Lm00000007mTYEAY'];
Adjust using the LIMIT 1 parameter
QFH = [select ID, Stop_Time__c, Name from Quote_Field_History__c where Id = 'a2Lm00000007mTYEAY' LIMIT 1];
or change the declaration
List<Quote_Field_History__c> checkQFH  = [select ID, Stop_Time__c, Name from Quote_Field_History__c where Id = 'a2Lm00000007mTYEAY'];

System.assertEquals(1,checkQFH.size(),'More than 1 QFH was returned');
System.assertNotEquals(null, checkQFH[0].Stop_Time__c, 'Stop Time is null which is not expected');

Some other feed back.
1.  Hard coding Ids in your test code is not best practice.
2. Your assert on the QFH.Stop_Time__c has no real value.  Salesforce basically promises to save your records for you.  Since you have set the field in the test class, there is little value in assert that Salesforce managed to save it for you.  Your assert should be testing your logic.


regards
Andrew

 
vishal-negandhivishal-negandhi

Hi Renee, 

Change your query 

 

QFH = [select ID, Stop_Time__c, Name from Quote_Field_History__c where Id = 'a2Lm00000007mTYEAY'];

SHOULD BE

QFH = [select ID, Stop_Time__c, Name from Quote_Field_History__c];


For the Test QFH data you're creating, may be you need a valid quote id in the field Quote__c but looks like it's just a text field. 
So the above change in query should fix your issue.

Grace TrueGrace True
Hi reenie, 
i think you should contact to a developer for better explaination to solve this error
"Getting the subject line error when validating my test class."
any way tell us more if you have some other query related to it  tellpizzahut (https://www.tellpizzahut.one/)
Renee NicholusRenee Nicholus
Andrew, I tried using the Limit, but got the same error.  I then tried the second change you suggested, and got this error: System.AssertException: Assertion Failed: More than 1 QFH was returned: Expected: 1, Actual: 0

@isTest
private class BusinessHoursTest {
    
    public static testMethod void BusinessHours(){        
        //Delcare var
        Decimal diff;
        
        //Test QFH data
        Quote_Field_History__c QFH = new Quote_Field_History__c();
        QFH.Quote__c = 'a2Lm00000007mTYEAY';
        QFH.Start_Time__c = system.now();
        QFH.Stop_Time__c = (system.now()+5);
        insert QFH;               
        
        
        system.debug('Inserting QFH with vaild QFH.Stop_Time__c value');
        
        BusinessHours BusinessHoursId = [SELECT Id FROM BusinessHours WHERE IsActive=true AND name= 'CS'];
        List<Quote_Field_History__c> checkQFH  = [select ID, Stop_Time__c, Name from Quote_Field_History__c where Id = 'a2Lm00000007mTYEAY'];

System.assertEquals(1,checkQFH.size(),'More than 1 QFH was returned');
System.assertNotEquals(null, checkQFH[0].Stop_Time__c, 'Stop Time is null which is not expected');
    }
}
Mayur Gore 10Mayur Gore 10

Hi Renee Nicholus,

                           
  1. You are getting error because your last query returning zero records. And your are assigning to object. so getting  no rows for assignment to SObject error
                             
2. I think there is correction in where filter of second query (use Quote__c   insted of Id)

               QFH = [select ID, Stop_Time__c, Name from Quote_Field_History__c where Quote__c  = 'a2Lm00000007mTYEAY'];
                             
 3. If you have assigned same Quote__c  = 'a2Lm00000007mTYEAY' to multiple records then use 
 QFH = [select ID, Stop_Time__c, Name from Quote_Field_History__c where Quote__c  = 'a2Lm00000007mTYEAY' limit 1];
   

@isTest
private class BusinessHoursTest {
    
    public static testMethod void BusinessHours(){        
        //Delcare var
        Decimal diff;
        
        //Test QFH data
        Quote_Field_History__c QFH = new Quote_Field_History__c();
        QFH.Quote__c = 'a2Lm00000007mTYEAY';
        QFH.Start_Time__c = system.now();
        QFH.Stop_Time__c = (system.now()+5);
        insert QFH;               
        
        
        system.debug('Inserting QFH with vaild QFH.Stop_Time__c value');
        
        BusinessHours BusinessHoursId = [SELECT Id FROM BusinessHours WHERE IsActive=true AND name= 'CS];
        QFH = [select ID, Stop_Time__c, Name from Quote_Field_History__c where Quote__c  = 'a2Lm00000007mTYEAY'];
        system.assert(QFH.Stop_Time__c != Null);
    }
}

Andrew GAndrew G
Since you are getting weird errors, possibly based on the code that is running of which we have seen nothing, i would look to a full test class rewrite as follows, where we control all the values being inserted and get rid of the hard coded Ids. 
 
@isTest
private class BusinessHoursTest {
   
    @Istest
    public static void BusinessHours(){        
        //Declare var
        Decimal diff;

        Account acc = new Account(Name='Test Account', Industry = 'Industrial', Rating = 'Cold'
			//other values as required
	);
	Insert acc;

	Opportunity opp = new Opportunity(Name = 'something', AccountId = acc.Id, stageName='Prospecting',closeDate=System.today()+5
		//other values as required
	);
	Insert opp;

        Quote quote = new Quote(Opportunity.Id = opp.Id);
        insert quote;
        
        //Test QFH data
        Quote_Field_History__c QFH = new Quote_Field_History__c();
        QFH.Quote__c = quote.Id;
        QFH.Start_Time__c = system.now();
        QFH.Stop_Time__c = (system.now()+5);
        insert QFH;               
        
        system.debug('Inserting QFH with vaild QFH.Stop_Time__c value');
        
        BusinessHours BusinessHoursId = [SELECT Id FROM BusinessHours WHERE IsActive=true AND name= 'CS];
        List<Quote_Field_History> qfhs = [SELECT Id from Quote_Field_History__c where Id = :QFH.Id];

//this assert does nothing more that prove that Salesforce inserted the record and that we got one back
	System.assertEquals(1,qfhs.size());  

	//now do some real asserts based on what you expect the code you are testing will do

    }
}

And as I now reread the code you proposed, the initial issue is that the QFH record is related to the hard coded Id which I assume is a quote record.   When we do the SELECT statement we are asking for QFH where the ID is the same as the Quote Id - which obviously it won't be.

Try the above test class and see if that at least gets you to point one.

regards
Andrew

p.s. all code provided as-is and uncompiled.