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
Jenna HildebrandJenna Hildebrand 

Help with Test Class Error – System.QueryException: List has no rows for assignment to SObject

Hi,

Could someone help me get past this error for the test class below?

System.QueryException: List has no rows for assignment to SObject
​Class.AccountTerritoryManagementTest.validateTerritoryManagement: line 13, column 1
 
@isTest
private class AccountTerritoryManagementTest {

    static testMethod void validateTerritoryManagement() {
    
    Account a = new Account(Name='Territory Testing', BillingState='LA', BillingPostalCode='12345', Account_Territory__c = null);
    System.debug('Territory = ' + a.Account_Territory__c);
    
    insert a;
    
    a = [SELECT Account_Territory__c, BillingPostalCode FROM Account WHERE Id =:a.Id];
    
    Zip_Code__c zipCode = [Select Name, Territory__c from Zip_Code__c where Name =:a.BillingPostalCode];
    
    System.debug('Territory after trigger fired: ' + a.Account_Territory__c);
        
    System.assertEquals(zipCode.Territory__c, a.Account_Territory__c);
    
    }

}
I want to deploy a new apex class to Production, but I'm having issues because its test class keeping failing. Any advice or insight would be greatly appreciated.

Thank you!
HARSHIL U PARIKHHARSHIL U PARIKH
Try using the following test class:
 
@isTest
private class AccountTerritoryManagementTest {

    static testMethod void validateTerritoryManagement() 
    {
    
        Account a = new Account();
        a.Name = 'Territory Testing';
        a.BillingState = 'LA';
        a.BillingPostalCode = '70001';
        a.Account_Territory__c = NULL;
        insert a;
        
        System.debug('Territory = ' + a.Account_Territory__c);
                
        List<Account> recentlyAddedAct = [SELECT Account_Territory__c, BillingPostalCode FROM Account WHERE Id =:a.Id];
        
        Zip_Code__c zipCode = [Select Name, Territory__c from Zip_Code__c where Name =:a.BillingPostalCode];
        
        System.debug('Territory after trigger fired: ' + a.Account_Territory__c);
            
        System.assertEquals(zipCode.Territory__c, a.Account_Territory__c);
    
    }

}

Hope it helps and it solves the puzzle then please mark it as best answer!
HARSHIL U PARIKHHARSHIL U PARIKH
You can also change the line 18 to the following:
 
List<Zip_Code__c> zipCode = [Select Name, Territory__c from Zip_Code__c where Name =:a.BillingPostalCode];

 
Akhilesh Reddy BaddigamAkhilesh Reddy Baddigam
@isTest
private class AccountTerritoryManagementTest {

    static testMethod void validateTerritoryManagement() {
    
    Account a = new Account(Name='Territory Testing', BillingState='LA', BillingPostalCode='12345', Account_Territory__c = null);
    System.debug('Territory = ' + a.Account_Territory__c);
    
    insert a;

    Zip_Code__c z=new Zip_Code__c(name='12345',Territory__c='z'); 
    insert z;


    a = [SELECT Account_Territory__c, BillingPostalCode FROM Account WHERE Id =:a.Id];
    
    Zip_Code__c zipCode = [Select Name, Territory__c from Zip_Code__c where Name =:a.BillingPostalCode limit 1];
    
    System.debug('Territory after trigger fired: ' + a.Account_Territory__c);
        
    System.assertEquals(zipCode.Territory__c, a.Account_Territory__c);
    
    }

}
hi jenna in line 13 you are querying zip_Ccode__c object which you havent inserted in the test class, just like the account that you inserted please try to insert the zipcode with name as account billing postal code and query it 

if this helpss to resolve your issue, please choose this as best answer. 
Thank you!
 
HARSHIL U PARIKHHARSHIL U PARIKH
Hmm.. I think Akhilesh is correct! I have completely overlooked that one :)
Jenna HildebrandJenna Hildebrand
Akhilesh and Harshil, thank you for your help! This makes sense.

Does this mean I also need to insert a Territory record since Territory__c on Zip_Code__c is a Territory lookup field? Because I am now receiving the following error:
 
System.StringException: Invalid id: z
Class.AccountTerritoryManagementTest.validateTerritoryManagement: line 11, column 1
 
Akhilesh Reddy BaddigamAkhilesh Reddy Baddigam
Yes, Jenna try to insert territroy also in the test class and give the same name in your zip_code__c!

let me know if you have any other questions thank you!
Jenna HildebrandJenna Hildebrand
Akhilesh, thank you! I hit an error because my System.assertEquals line wasn't returning equal values. But, I found the fix! Originally I was creating a new Zip_Code__c variable (zipCode) to pull the name and territory where the name equaled the account zip code, and I needed to replace zipCode with the new Zip_Code__c record inserted (z) and pull from there instead. Here is what the functioning code looks like now:
@isTest
private class AccountTerritoryManagementTest {

    static testMethod void validateTerritoryManagement() {
    
    Territory__c t = new Territory__c(Name='Test Territory');
    
    insert t;
    
    t = [SELECT Id FROM Territory__c WHERE Name='Test Territory'];
    
    Zip_Code__c z = new Zip_Code__c(Name='12345', Territory__c=t.Id);
    
    insert z;
    
    Account a = new Account(Name='Territory Testing', BillingState='LA', BillingPostalCode='12345', Account_Territory__c = null);
    System.debug('Territory = ' + a.Account_Territory__c);
    
    insert a;
    
    a = [SELECT Account_Territory__c, BillingPostalCode FROM Account WHERE Id =:a.Id];
    
    z = [Select Name, Territory__c from Zip_Code__c where Name =:a.BillingPostalCode limit 1];
    
    System.debug('Territory after trigger fired: ' + a.Account_Territory__c);
        
    System.assertEquals(z.Territory__c, a.Account_Territory__c);
    
    }

}
Pretty sure this is right, but let me know if you see any errors I should learn from. Thank you again for your help!