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
Miguel RoaMiguel Roa 

Custom object won't return anything

Hi, I'm new in APEX and I've been told to fix this test class:
 
@Istest
Public class MillisecondBatchConvertController_Test{
public static testMethod void testBatch() {
       
    Team_Form__c tfCustom = new Team_Form__c();
    tfCustom = [Select Id from Team_Form__c LIMIT 1];
    List <PS_TF_Status_Track__c> stList = new List<PS_TF_Status_Track__c>();
    for(integer count = 0; count<=1; count++){
        PS_TF_Status_Track__c st = new PS_TF_Status_Track__c(Hours__c=count,Minutes__c=count,Days__c=count,Team_Form__c=tfCustom.Id);
     stList.add(st);}
       
        insert stList;
       
        Test.startTest();
        MillisecondBatchConvertController mbcCon=new MillisecondBatchConvertController();
        ID batchprocessid = Database.executeBatch(mbcCon,1);
        Test.stopTest();
    }
}


And I'm getting this error:

System.QueryException: List has no rows for assignment to SObject

I suppose this is because tfCustom.Id is empty but why is doesn't have any value?

Thanks in advance

Best Answer chosen by Miguel Roa
ANUTEJANUTEJ (Salesforce Developers) 
Hi Migual,

The error states that there is no records that were returned from the soql query.

In test classes by default there is no access to the data in the org so you need to insert test data so before the soql you need to insert a record for Team_Form__c  object. After inserting the record you can use fetch and perform the rest of the operations.

Try changing the below lines:
 
Team_Form__c tfCustom = new Team_Form__c();
tfCustom = [Select Id from Team_Form__c LIMIT 1];

to something as below:
Team_Form__c test_record = new Team_Form__c ();
test_record.name='test'; //Here I am giving only name field value but you need to insert the required fields and the necessary ones you are using in the test class
insert test_record;
Team_Form__c tfCustom = new Team_Form__c();
tfCustom = [Select Id from Team_Form__c LIMIT 1];

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Migual,

The error states that there is no records that were returned from the soql query.

In test classes by default there is no access to the data in the org so you need to insert test data so before the soql you need to insert a record for Team_Form__c  object. After inserting the record you can use fetch and perform the rest of the operations.

Try changing the below lines:
 
Team_Form__c tfCustom = new Team_Form__c();
tfCustom = [Select Id from Team_Form__c LIMIT 1];

to something as below:
Team_Form__c test_record = new Team_Form__c ();
test_record.name='test'; //Here I am giving only name field value but you need to insert the required fields and the necessary ones you are using in the test class
insert test_record;
Team_Form__c tfCustom = new Team_Form__c();
tfCustom = [Select Id from Team_Form__c LIMIT 1];

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
This was selected as the best answer
Andrew GAndrew G
You could also scare people and stick some System.asserts in there.

regards
Andrew