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
Pavan Kumar 1141Pavan Kumar 1141 

Need Help on Test Coverage. Not able to pass the test class

Hi All, Need Help on code coverage for below apex class. Getting List has no rows to assign to sobject. Any help would be great. Thank you.

Here is my apex code

public class Instructions {
    public Account acc{set;get;}
    public BGBK__Return__c annualReport{get;set;}
    public Instructions(){
        acc = [select id,Business_Type__c from Account where Id=: ApexPages.currentPage().getParameters().get('entity')];
        annualReport = [Select Id,Read_Instructions__c from BGBK__Return__c where Account__c =: ApexPages.currentPage().getParameters().get('entity') Order by CreatedDate DESC limit 1];
    }
    
    public PageReference goBack()
    {        
        annualReportSubmissionLinks ARSL = new annualReportSubmissionLinks();
        Account arslAccount = ARSL.getAccount();  
       
        if(arslAccount.Business_type__c == ViewAccount.FINANCIAL_INSTITUTION || arslAccount.Business_type__c == ViewAccount.INSURANCE){
            PageReference pr = new PageReference('/apex/annualreportsubmissionlinks?entity='+ ApexPages.currentPage().getParameters().get('entity'));
            return pr;
        }else{
            PageReference pr = new PageReference('/apex/annualreportsubmissionlinks?entity='+ ApexPages.currentPage().getParameters().get('entity')+'&licenseId='+ApexPages.currentPage().getParameters().get('licenseId'));
            return pr;
        }
        
    }
    
    public PageReference confirm()
    {        
        annualReportSubmissionLinks ARSL = new annualReportSubmissionLinks();
        List<BGBK__Return__c> annualReports = ARSL.getAnnualReports(); 
        Account arslAccount = ARSL.getAccount();       
        List<MUSW__License2__c> licensesList = ARSL.getLicenses();
        
        if(arslAccount.Business_type__c == ViewAccount.FINANCIAL_INSTITUTION || arslAccount.Business_type__c == ViewAccount.INSURANCE){          
            annualReports[0].Read_Instructions__c = annualReport.Read_Instructions__c;
            annualReports[0].Submission_Status__c = UploadCSV2.SUBMISSION_PROGRESS;
            update annualReports[0];
            PageReference pr = new PageReference('/apex/annualreportsubmissionlinks?entity='+ ApexPages.currentPage().getParameters().get('entity'));
            return pr;
        }else{             
            annualReports[0].Read_Instructions__c = annualReport.Read_Instructions__c;
            annualReports[0].Submission_Status__c = UploadCSV2.SUBMISSION_PROGRESS;
            update annualReports[0];
            PageReference pr = new PageReference('/apex/annualreportsubmissionlinks?entity='+ ApexPages.currentPage().getParameters().get('entity')+'&licenseId='+ApexPages.currentPage().getParameters().get('licenseId'));
            return pr;
        }
        //if(accList.size()>0 && licensesList.size()>0)
    }
}
AnudeepAnudeep (Salesforce Developers) 
The error "List has no rows for assignment to SObject" occurs when the query doesn't return any rows.

Resolution 
 
While a SELECT normally returns an array/list, these statements are using the shorthand syntax that assumes only one row is returned. What’s not obvious is that it also assumes that exactly one row is returned!
 
Although this is unlikely to occur for Contact, it is highly likely to occur for any custom objects you create, especially when a WHERE statement is used that might return zero rows, such as:
 
Player__c player = [SELECT Id from Player__c where Name = :username]; if (player != null)  p = player.Id;
The above code will fail if there is no Player__c record with the matching username. It doesn't actually return a null.

It would be safer to do the following:
 
Player__c[] players = [SELECT Id from Player__c where Name = :username]; if (players.size() > 0) p = players[0].Id;

https://help.salesforce.com/articleView?id=000328824&type=1&mode=1