+ Start a Discussion
samruddhi podesamruddhi pode 

Getting visualforce page error as List has no rows for assignment to SObject An unexpected error has occurred.

Getting visualforce page error as List has no rows for assignment to SObject.  An unexpected error has occurred. Your development organization has been notified.
Here is my page controller.

Controller-
public class MyFirstController {


    private final Account account;

    public MyFirstController () {

        account = [SELECT Id, Name, Site FROM Account

                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];

    }

 
    public Account getAccount() {

        return account;

    }

 
    public PageReference save() {

        update account;

        return null;

    }

}
 
Ravikant Saini 5Ravikant Saini 5
The exception is because of you are not getting any record in your SOQL query and in case of no records it returns an empty list.
Its always a best practive to use List<Account> at place of Account.
sfdcMonkey.comsfdcMonkey.com
Hello samruddhi pode
you got this error becasue your query not getting any single record
Soql always return List so use below code for handle it 
public class MyFirstController {
    private final List<Account> OAccount;
    public MyFirstController () {
        OAccount = [SELECT Id, Name, Site FROM Account
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
      }

 
    public List<Account> getAccount() {
      
	  List<account> lstOfQueryAccount = new List<Account>();
        for(account acc:OAccount ){
		 lstOfQueryAccount.add(acc);
		}
		
		return lstOfQueryAccount;

    }

 
    public PageReference save() {

        update OAccount;

        return null;

    }

}
Thanks
let me inform if it helps you And Mark it best answer so it make proper solution for others in future :)
 
MoggyMoggy
I think you are missing the essential bit of a contructor for the use with VF
public MyFirstController(ApexPages.StandardController std){
 account = [Select Id,Name,Site From Account where Id =: ApexPages.currentPage()getParameters().get('Id')];

/*
This also assumes that your Visual force page uses standardController='Account' extensions='MyFirstController'

this ensures that this page can only be used within account layouts so you always have an account ID

if that is a controller on its own, then you need to add
*/
if(null != ApexPages.currentPage()getParameters().get('Id'){
 // you might also need to check if the ID starts with 001 if its the only controller on the page
account = [Select Id,Name,Site From Account where Id =: ApexPages.currentPage()getParameters().get('Id')];
}