You need to sign in to do that
Don't have an account?
Pavan 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)
}
}
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)
}
}
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:
It would be safer to do the following:
https://help.salesforce.com/articleView?id=000328824&type=1&mode=1