You need to sign in to do that
Don't have an account?
Renee 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);
}
}
@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);
}
}
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
Hi Renee,
Change your query
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.
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/)
@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');
}
}
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);
}
}
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.