4

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;
    }
}
0

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 '14 at 8:55
5

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.

2

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.