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
Jennifer.SchnellJennifer.Schnell 

Apex Test Failure - System.QueryException: List has no rows for assignment to SObject

I am receiving an apex test error - the error message is:
System.QueryException: List has no rows for assignment to SObject
Stack Trace: Class.BillingScheduleTest.TestBillingSchedule2: line 108, column 1

When I view the code in the developer console - line 108 code is the below:
        Asset objAS = [Select Id,Contract__c,Name  From Asset Where Name= 'TestAsset' Limit 1];

Still learning a lot of this stuff, but is the error telling me that I don't have an Asset with the name "TestAsset"?

Thanks.
Dev_AryaDev_Arya
Hi Jennifer,

Did you create the test record in your test class? Because test classes can not access the org data untill or unless explicitly instructed to do so (@isTest(setALlData = True)). Secondly one should retrieve the SOQL records in a list variable because SOQL returns a list of the sObjects even if it fetches one record. For example :
// instead of this
Asset objAS = [Select Id,Contract__c,Name  From Asset Where Name= 'TestAsset' Limit 1];

// use this
List<Asset> objAS = [Select Id,Contract__c,Name  From Asset Where Name= 'TestAsset' Limit 1];
if(objAS.size() > 0){
  // Do Something;
}

Using a list also helps you to check a condition over the size of the list (as mentioned in the code above) and prevents to fall in to nullpointer exception trap. 

Hope this helps.

Cheers,
Dev
Jennifer.SchnellJennifer.Schnell
Thanks for the reply Dev.  Here is the entire code
@isTest(SeeAllData=true)
private class BillingScheduleTest{    
  /*  @testSetup static void TestBillingSchedule(){
    
        Account objAccount = new Account();
        objAccount.Name = 'TestAccount';
        objAccount.Phone = '1234567890';
        objAccount.CurrencyIsoCode ='USD';
        insert objAccount;
        
        Contact objContact = new Contact();
        objContact.LastName = 'test';
        objContact.AccountId = objAccount.Id;
        objContact.Contact_Origination__c = 'LLamasoft';
        objContact.LeadSource = 'Advertisement';
        objContact.Email = 'test@gmail.com';
        objContact.Phone = '1234567890';        
        insert objContact;  
        
        Contract objContract = new Contract();
        objContract.AccountId = objAccount.Id;
        objContract.Status = 'Draft';
        objContract.ContractTerm = 1234;
        objContract.StartDate = System.Today();
        objContract.CustomerSignedId = objContact.Id;
        insert objContract;
        
        Id RecordTypeAssetId = Schema.SObjectType.Asset.getRecordTypeInfosByName().get('License Purchase Detail').getRecordTypeId();
        Asset objAsset = new Asset();
        objAsset.AccountId = objAccount.Id;
        objAsset.Contract__c = objContract.Id;
        objAsset.Name = 'TestAsset';
        objAsset.CurrencyIsoCode = 'USD';
        objAsset.RecordTypeId = RecordTypeAssetId;
        insert objAsset;
        
        Billing_Schedule__c objBillingSchedule = new Billing_Schedule__c();
        objBillingSchedule.Name = 'TestBillingSchedule';
        objBillingSchedule.Asset__c =  objAsset.Id;
        objBillingSchedule.Contract__c = objContract.Id;
        objBillingSchedule.CurrencyIsoCode = 'USD';
        objBillingSchedule.Start_Date__c = System.Today();
        objBillingSchedule.End_Date__c = System.Today();
        insert objBillingSchedule;
        
        Opportunity objOpportunity = new Opportunity();
        objOpportunity.Name= 'TestOpportunity';
        objOpportunity.Account= objAccount;
        objOpportunity.Type = 'Renewal';
        objOpportunity.LeadSource = 'Advertisement';
        objOpportunity.CloseDate = System.Today();
        objOpportunity.StageName = 'Prospect';
        objOpportunity.CurrencyIsoCode = 'USD';
        objOpportunity.Next_Steps__c = 'Test Next Step';
        insert objOpportunity;
        
        Id pricebookId = Test.getStandardPricebookId();
         
        Product2 objProduct = new Product2();
        objProduct.Name = 'DataGuru Consulting';
        objProduct.IsActive = true;
        insert objProduct;
        
        PricebookEntry objPricebookEntry =new PricebookEntry();
        objPricebookEntry.Pricebook2Id = pricebookId;
        objPricebookEntry.Product2Id = objProduct.Id;
        objPricebookEntry.UnitPrice = 100.00;
        objPricebookEntry.IsActive = true;
        insert objPricebookEntry;
        
        System.assertEquals('TestBillingSchedule',objBillingSchedule.Name);
        
        ApexPages.StandardController controller =new ApexPages.StandardController(objAsset);
        BillingSchedule objBillingSchedule1 = new BillingSchedule(controller);
        objBillingSchedule1.addNewBilling();
        objBillingSchedule1.findReletedAssetBillingSchedule();
        objBillingSchedule1.BindBillingSchedule();
        objBillingSchedule1.removeIndex =0;
        objBillingSchedule1.removeNewBilling();
        objBillingSchedule1.saveBilling();
    }
    */
    static testmethod void TestBillingSchedule1(){
        
        Asset objAS = [Select Id,Contract__c  From Asset Where Name= 'TestAsset' Limit 1]; 
        
        Billing_Schedule__c objBillingSchedule = new Billing_Schedule__c();
        objBillingSchedule.Name = 'TestBillingSchedule';
        objBillingSchedule.Asset__c =  objAS.Id;
        objBillingSchedule.Contract__c = objAS.Contract__c;
        objBillingSchedule.CurrencyIsoCode = 'USD';
        objBillingSchedule.Start_Date__c = System.Today();
        objBillingSchedule.End_Date__c = System.Today().addDays(+15);
        /* Gears DEV - added required payment terms field for object creation and testing. Case 32655 */
        objBillingSchedule.Payment_Terms__c = 30;
        insert objBillingSchedule;
               
        ApexPages.StandardController controller =new ApexPages.StandardController(objAS);
        BillingSchedule objBillingSchedule1 = new BillingSchedule(controller);
        objBillingSchedule1.saveBilling();
       
        List<Billing_Schedule__c> BillingSchedulesobj = new List<Billing_Schedule__c>([select id,Name From Billing_Schedule__c where Asset__c =: objAS.Id Order by Start_Date__c asc]);

        System.assertEquals('Period 1 - '+ objBillingSchedule1.formatDate(objBillingSchedule.Start_Date__c)+ ' - '+objBillingSchedule1.formatDate(objBillingSchedule.End_Date__c),BillingSchedulesobj[0].Name);
    }
    static testmethod void TestBillingSchedule2(){
        
        Asset objAS = [Select Id,Contract__c,Name  From Asset Where Name= 'TestAsset' Limit 1]; 
                       
        ApexPages.StandardController controller =new ApexPages.StandardController(objAS);
        BillingSchedule objBillingSchedule1 = new BillingSchedule(controller);
        objBillingSchedule1.BindBillingSchedule();
        System.assertEquals('TestAsset',objAS.Name);
    }
}

 
Suraj TripathiSuraj Tripathi

Hi Jennifer,

Please make sure you have Asset record named "TestAsset" with Contract__c.

First you add @isTest(SeeAllData=true) above the  TestBillingSchedule2() method Like:
 

@isTest(SeeAllData=true)
static testmethod void TestBillingSchedule2(){


try this hope it will help you.

For More information:-  https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_seealldata_using.htm

Mark as a best if it helps you.

Regards,

Suraj

Dev_AryaDev_Arya
Hi Jennifer,

I looked in to your code, it seems correct to me. Could you please post the error along with the trace, that will help to dig more. 

Viele Grüße,
Dev 
Jennifer.SchnellJennifer.Schnell
Class Name:  BillingScheduleTest
Method:  TestBillingSchedule2
Error Message:  System.QueryException: List has no rows for assignment to SObject 
Stack Trace: Class.BillingScheduleTest.TestBillingSchedule2: line 108, column 1
Jennifer.SchnellJennifer.Schnell
Also - here is the entire code - gist.github.com/JennSchnell/1fe9ab2fc2288ad5077baa15ae61ddfa