+ Start a Discussion
sfdotcomsfdotcom 

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

Hi, I wrote an exension, which is used to retrieve the data from from another object to Case page based on picklist value "Type". "Type" picklist has two values. They are 1. Regular  2. Contract. If Regular is selected then it fetches the related data. For this i have created a value in other object Employee. But when i select Type as "Contract". Then it's giving me error. Because I haven't given any value for "Contract" in Employee object.
 Please help me on fixing this error.

Controller Extension:
Public class TestCase_Controller{
    Public Case caseData {get; set;}
    Public Member__c memberData {get; set;}
    Public Employee__c employeeData {get; set;}
    Id accId {get; set;}
  //  Id recordTypeValue {get; set;}
    
    Public TestCase_Controller(ApexPages.StandardController stdCtr){
        caseData = (Case)stdCtr.getRecord();
        memberData = new Member__c ();
        employeeData = new Employee__c();
        accId = ApexPages.CurrentPage().getParameters().get('AccountId');
   //     recordTypeValue = ApexPages.currentPage().getParameters().get('RecordType');
        caseData.AccountId = accId;
       // caseData.RecordTypeId = recordTypeValue;
    }
    
    Public void populateMemberDetails(){
    if(!String.isBlank(caseData.Member__c)){
    memberData = [Select ID, Description__c from Member__c where ID = :caseData.Member__c ];
    caseData.Description = memberData.Description__c ; 
    }
     else
        {
            memberData=null;
            caseData.Description = null;
        }
   
    }
    
     Public void populateEmployeeDetails(){
    if((!String.isBlank(caseData.Type__c) )){
    employeeData = [Select ID, Employee_Description__c, Type__c from Employee__c where Type__c = :caseData.Type__c ];
   
    caseData.Employee_Description__c = employeeData.Employee_Description__c ; 
    }
    
     else
        {
            employeeData.type__c =null;
            caseData.Employee_Description__c = null;
        }
   
    }
    
    
    Public pageReference customSave(){
        Try{
        
            Upsert caseData;
            Return new PageReference('/' +caseData.Id);
              }
        Catch(Exception e){
            ApexPages.addMessages(e);
            Return null;
        }
        
        }
    }

Error:
Visualforce ErrorHelp for this Page
System.QueryException: List has no rows for assignment to SObject
Error is in expression '{!populateEmployeeDetails}' in page testcase: Class.TestCase_Controller.populateEmployeeDetails: line 33, column 1
Class.TestCase_Controller.populateEmployeeDetails: line 33, column 1
sfdotcomsfdotcom
If Case Type is Regular, and if there is no record with Regular in Employee, then it will display error. If there is record then it will display value.

I am getting error in below piece of code:

 Public void populateEmployeeDetails(){
    if((!String.isBlank(caseData.Type__c) )){
    employeeData = [Select ID, Employee_Description__c, Type__c from Employee__c where Type__c = :caseData.Type__c ];
   
    caseData.Employee_Description__c = employeeData.Employee_Description__c ; 
    }
    
     else
        {
            employeeData.type__c =null;
            caseData.Employee_Description__c = null;
        }
   
    }
AnjithKumarAnjithKumar
Hi,
 
Public class TestCase_Controller{
    Public Case caseData {get; set;}
    Public Member__c memberData {get; set;}
    Public Employee__c employeeData {get; set;}
    Id accId {get; set;}
  //  Id recordTypeValue {get; set;}
    
    Public TestCase_Controller(ApexPages.StandardController stdCtr){
        caseData = (Case)stdCtr.getRecord();
        memberData = new Member__c ();
        employeeData = new Employee__c();
        accId = ApexPages.CurrentPage().getParameters().get('AccountId');
   //     recordTypeValue = ApexPages.currentPage().getParameters().get('RecordType');
        caseData.AccountId = accId;
       // caseData.RecordTypeId = recordTypeValue;
    }
    
    Public void populateMemberDetails(){
    if(!String.isBlank(caseData.Member__c)){
    memberData = [Select ID, Description__c from Member__c where ID = :caseData.Member__c ];
    caseData.Description = memberData.Description__c ; 
    }
     else
        {
            memberData=null;
            caseData.Description = null;
        }
   
    }
    
     Public void populateEmployeeDetails(){
    if((!String.isBlank(caseData.Type__c) )){
List<Employee__c> empList = [Select ID, Employee_Description__c, Type__c from Employee__c where Type__c = :caseData.Type__c limit 1 ];
   if(empList.size()>0){

    employeeData =empList [0];
}
    caseData.Employee_Description__c = employeeData.Employee_Description__c ; 
    }
    
     else
        {
            employeeData.type__c =null;
            caseData.Employee_Description__c = null;
        }
   
    }
    
    
    Public pageReference customSave(){
        Try{
        
            Upsert caseData;
            Return new PageReference('/' +caseData.Id);
              }
        Catch(Exception e){
            ApexPages.addMessages(e);
            Return null;
        }
        
        }
    }

Let me know if it works.

Thanks,
Anjith
KapilCKapilC
Hi

Could you please share your VF page code as well.

Thanks,
Kapil
sfdotcomsfdotcom
Hi Anjith,
 Now it is working fine. I am not getting any error. But when select type = 'Regular' then it is displaying value. But when i change Type value from Regular to Contract, still it is displaying the value of Regular. Contract value should be null. because we don't have any record inserted related to Contract. I think we have to use clearResult(). I am not aware of this method..
 
AnjithKumarAnjithKumar
Could you share you Vf page code as well..
sfdotcomsfdotcom
Hi Anjith,
If we select Type__c = 'Regular', then it fetches value of  "employeeData.Employee_Description__c" like as "Regular Description". If I select Type__c = 'Contract', then it is showing same value "Regular Description". But for Type__c = 'Contract', I didn't create any record in Employee object. So, it should not display any value. I hope u got my point.

Here is the VF page:

<apex:page standardController="Case" extensions="TestCase_Controller" sidebar="false">
<apex:form >
<apex:pagemessages />
<apex:actionFunction name="populateMemberData" action="{!populateMemberDetails}" rerender="ajaxrequest"/>
<apex:actionFunction name="populateEmployeeData" action="{!populateEmployeeDetails}" rerender="ajaxrequest1"/>
<apex:pageBlock title="Case Edit" mode="edit">
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!customSave}" />
<apex:commandButton value="Cancel" action="{!cancel}" />
</apex:pageBlockButtons>
<apex:pageBlockSection title="Case Detail" columns="2">
<apex:inputField value="{!caseData.AccountId}"/>
<apex:inputField value="{!caseData.Member__c}" onchange="populateMemberData()"/>
<apex:inputField value="{!caseData.Type__c}" onchange="populateEmployeeData()"/>
</apex:pageBlockSection> <apex:pageBlockSection id="ajaxrequest">
<apex:outputField value="{!caseData.Description}" />
</apex:pageBlockSection> <apex:pageBlockSection id="ajaxrequest1">
<apex:outputField value="{!caseData.Employee_Description__c}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
sfdotcomsfdotcom
Hi Anjith,
I have fixed it. It is working fine..

Thank you Anjith and Kapil
sfdotcomsfdotcom
Hi,

I wrote test class for above controller extension. I am not getting code coverage. Please help me..

Test class:

@isTest
public class TestCase_Controller_Test {
    static testMethod void testSave(){
    Test.setCurrentPageReference(Page.TestCase);
        Account acc = new Account (Name = 'Test');
        insert acc;
        
        Member__c mem = new Member__c(Name = 'Test', Description__c = 'Test');
        insert mem;
        
        Employee__c emp = new Employee__c (Name = 'Test', Type__c = 'Test', Employee_Description__c = 'Test', Email__c = 'sohelmd060@gmail.com');
        insert emp;
        
        Case cs = new Case (Status = 'New', Origin = 'Phone', Description = 'Test', Member__c = mem.Id, Employee__c = emp.Id);
       
        insert cs;
    
        ApexPages.StandardController stdCtr = new ApexPages.StandardController(cs);
        TestCase_Controller tc = new TestCase_Controller(stdctr);
        tc.populateMemberDetails();
        tc.customSave();
        tc.employeeData = emp;
        tc.populateEmployeeDetails();
      
      //  tc.accountId = acc.Id;
        
        tc.caseData = cs;
        tc.memberData = mem;
        system.assertEquals(tc.caseData.Member__c, mem.id);
        system.assertEquals(tc.caseData.Employee__c, emp.Id);
        system.assertEquals(tc.caseData.Type__c, emp.Type__c);
        system.assertEquals(tc.caseData.Email__c, emp.Email__c);
        system.assertEquals(tc.caseData.Description, mem.Description__c);
  
        
    }

}

Thank You
sfdotcomsfdotcom
Hi, I wrote test class for above controller extension. I am not getting code coverage. Please help me.. Test class: @isTest public class TestCase_Controller_Test { static testMethod void testSave(){ Test.setCurrentPageReference(Page.TestCase); Account acc = new Account (Name = 'Test'); insert acc; Member__c mem = new Member__c(Name = 'Test', Description__c = 'Test'); insert mem; Employee__c emp = new Employee__c (Name = 'Test', Type__c = 'Test', Employee_Description__c = 'Test', Email__c = 'sohelmd060@gmail.com'); insert emp; Case cs = new Case (Status = 'New', Origin = 'Phone', Description = 'Test', Member__c = mem.Id, Employee__c = emp.Id); insert cs; ApexPages.StandardController stdCtr = new ApexPages.StandardController(cs); TestCase_Controller tc = new TestCase_Controller(stdctr); tc.populateMemberDetails(); tc.customSave(); tc.employeeData = emp; tc.populateEmployeeDetails(); // tc.accountId = acc.Id; tc.caseData = cs; tc.memberData = mem; system.assertEquals(tc.caseData.Member__c, mem.id); system.assertEquals(tc.caseData.Employee__c, emp.Id); system.assertEquals(tc.caseData.Type__c, emp.Type__c); system.assertEquals(tc.caseData.Email__c, emp.Email__c); system.assertEquals(tc.caseData.Description, mem.Description__c); } } Thank You