0

I am facing the below error in my test class.

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

I get this error on the following line in my test class:

InsolvencyPopupMessage Insolvpop = new InsolvencyPopupMessage(sc);

Apex Class

public class InsolvencyPopupMessage {
 public String insolvencyAccntId{get;set;}

public Boolean sentToCollections {get;set;}
 public Insolvency_Account__c insolvncyAccnt{get;set;}

public InsolvencyPopupMessage(ApexPages.StandardController controller){

        //Insolvency Account ID
        insolvencyAccntId = ApexPages.currentPage().getParameters().get('id');  

        //Insolvency Account record
insolvncyAccnt = [SELECT Id,Name,Accepted_Sent_to_Collections__c,Insolvency_Status__c  
                            FROM Insolvency_Account__c WHERE Id =: insolvencyAccntId limit 1];
 if(insolvncyAccnt.Accepted_Sent_to_Collections__c == true && insolvncyAccnt.Insolvency_Status__c!='Sent to collections'){
            senttocollections = true;
        }              
    }

public PageReference displayPopup() { 
    PageReference newPage;
    if(insolvncyAccnt.Insolvency_Status__c!='Sent to collections'){
            /*newPage = New PageReference('/apex/InsolvencyAccntPopUp');
          newPage.getParameters().put('id',ApexPages.currentPage().getParameters().get('id'));
          newPage.setRedirect(true);*/
          return null;

    }else{

        newPage = New PageReference('/apex/InsolvencyAccountView');
        newPage.getParameters().put('id',ApexPages.currentPage().getParameters().get('id'));
        newPage.setRedirect(true);
        return newPage;
    }

}
}

Test Class

@istest
public class TestInsolvencyPopupMessage {
@istest
    public static void TestInsolvencyPopupMessage()
    {
        Insolvency__c insolv=new Insolvency__c();
        insolv.Insolvency_Type__c='Bankruptcy';
        insolv.Insolvency_Status__c='Staging';
        insolv.Name='35-1415724';
        insolv.Debtor_1_Court_Number__c='12';
        insolv.Court_Division_Number__c='1014';
        insolv.Court_District_Name__c='101';
        insolv.Court_Province__c='Alberta';
        insolv.Insolvency_Received_Date__c=system.today();
        insolv.Insolvency_Date__c=system.today();
        insolv.Debtor_1_First_Name__c='dummy';
        insolv.Debtor_1_Last_Name__c='dummy2';
        insolv.Debtor_1_Date_of_Birth__c=Date.newInstance(1994, 12, 9);
        insolv.Debtor_1_Gender__c='Male';
        insolv.Debtor_1_Mailing_Street__c='dummy text';
        insolv.Debtor_1_Mailing_City__c='Toronto';
        insolv.Debtor_1_Mailing_State_Province__c='Ontario';
        insolv.Debtor_1_Mailing_Postal_Code__c='N2T2X6';
        Insert insolv;
        RecordType rt=[select id,Name from RecordType where Name='Scotia Bank Insolvency Account' and sObjectType='Insolvency_Account__c' limit 1];
        Insolvency_Account__c insolvncyAccnt=new Insolvency_Account__c();
        insolvncyAccnt.RecordTypeId=rt.id;
        insolvncyAccnt.Name='1056649437375';
        insolvncyAccnt.Account_Type__c='Unknown';

        insolvncyAccnt.Insolvency_Status__c='New';
        insolvncyAccnt.Borrower_Primary__c='test';
        insolvncyAccnt.Type_of_Service__c='Service and Sale';
        insolvncyAccnt.Claim_Type__c='Unsecured';
        Insert insolvncyAccnt;
        insolvncyAccnt.Insolvency_Status__c='Sent to collections';
        update insolvncyAccnt;    

        ApexPages.StandardController sc = new ApexPages.standardController(insolvncyAccnt);
        InsolvencyPopupMessage Insolvpop = new InsolvencyPopupMessage(sc);
        Insolvpop.displayPopup();     

    }
}

1 Answer 1

3

Your query relies on getting an Id from the page parameters, so your test needs to set that before your constructor is called.

No Id = query returns no rows. Combine that with assigning the result of a query to a single variable (instead of a collection), and you get the "List has no rows for assignment to SObject" error you're seeing.

I think the only way we currently have of setting page parameters is to get a reference to the page parameter map, and add your target parameter to it.

Map<String, String> pageParams = ApexPages.currentPage().getParameters();
// The key of the map here needs to be an exact match for what your controller/extension
//   is trying to use
pageParams.put('id', <your Id here>);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .