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
GhanesanGhanesan 

List has no rows for assignment to SObject - Apex/VfPage

Hi ,
Getting this error "List has no rows for assignment to SObject"
Trying to create button through vf page. When button is clicked, the date field should update. below is my code. 

Controller: 
public class ResignationButtonClass {
    public Employee_Information__c emp;
           public ResignationButtonClass() {
        emp = [SELECT DN_Employee_ID__c FROM Employee_Information__c 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
    }

    public Employee_Information__c getemp() {
        return emp;
    }
                   public PageReference Resign() {
                      pagereference redirect = new PageReference('/apex/newPage'); 
                      emp.DN_Resignation_Date__c= System.today();           
                      update emp;
                      return null;
                   
                  }
    
}


VF Page: 

<apex:page controller="ResignationButtonClass" >
 <apex:form >
 <apex:commandbutton value="Resign" action="{!Resign}"/>
           
       </apex:form> 
</apex:page>
Sai PraveenSai Praveen (Salesforce Developers) 
Hi ,

In the Vf you are just calling only one method which is Resign(). Do you mean the other methods were not needed in Aepx class?

Thanks,
 
Alejandra RochaAlejandra Rocha
Hi Ghanesan,

I can't tell which line of code you're getting this error in. But according to your code, this line might be the problem:

emp = [SELECT DN_Employee_ID__c FROM Employee_Information__c 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];

To avoid this error, you can

1 - Check that this statement returns the correct value: ApexPages.currentPage().getParameters().get('id')
2 - Check that the id returned by ApexPages.currentPage().getParameters().get('id') is an Employee_Information__c id.