I have this controller but I am getting this error

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

public class MyController {
    private final Account account;

    public MyController() {
        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;

3 Answers 3


I would agree with Akash here.

First, the 'account' instance must be initialised.

Second, just make sure the ID is being passed in the page's URL correctly. In the ApexPages.currentPage().getParameters().get('id') statement, the .get('id') part is case sensitive. So please make sure, that is how you have passed your ID in the URL.

Third, there should be a record in ur system with that ID.

Considering these three, it should work perfectly.

  • were u able to solve the issue? if yes, please mark one of the solutions as answers for further users' reference.
    – Sam
    Nov 13, 2014 at 8:55

This basically means your query doesn't return any results. It may be because there are not records for that ID or that ID is not passed in the URL.

Initialize the account before you hit the query.


This error indicates that your query returns no results. Salesforce throws this error when you assign a query result to a singular instance of an object. If you were assigning to a list (or map) this would not be the case, you would just be given an empty list.

To solve your current issue you should use a try/catch block around the query. Your assignment would be in the try and probably post a page message in the catch indicating to the user what the problem was.

    account = [YOUR QUERY];
} catch (Exception e) {
    // add your message to the user or
    account = new Account();

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .