0

I am trying to create a custom Visualforce page with a custom controller by following the example on this page:

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_custom.htm

However, when I view the page that uses the code given in the example, I get this error:

List has no rows for assignment to SObject An unexpected error has occurred. Your development organization has been notified.

I just copied and pasted the code from the example into a custom class and a custom visualforce page. How do I fix this issue? Thanks!

3 Answers 3

2

The error your receiving is due to a lack of an Account object in your current development organization.

Check to make sure you have at least 1 Account record that matches the id your supplying to the page via the url parameter that is being input to the page.

Maybe you could have the page output a random account object changing:

 public MyController() {
    account = [SELECT Id, Name, Site FROM Account 
               WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
}

to:

 public MyController() {
    account = [SELECT Id, Name, Site FROM Account LIMIT 1];
}
4
  • I do have an Account in my development organization. The user is a part of that account as well. How do I check what id is being passed and what the ids of the accounts I have?
    – Di Zou
    Sep 24, 2012 at 15:36
  • Well change the link to supply the ID of the record you want to view on this page. See the change I just made where I just select 1 record out of the database. Sep 24, 2012 at 15:37
  • supply the ID of the record you want to view on this page What do you mean by this? Do you mean that the URL needs to have the account id?
    – Di Zou
    Sep 24, 2012 at 15:41
  • Yes. .../apex/mypage?id=... after the equals sign place the Id of the record your trying to view. Sep 24, 2012 at 16:54
0

Di Zou

In the example its explicitly not referred that when you try to access the page

When Jordan says use /apex/mypage?Id=001234567890( some account Id: To get account Id click on any account record and you can copy the 18 digit Id associated from the resulting URL)

In the example when the author says

"ApexPages.currentPage().getParameters().get('id')" it means that he is trying to access the current page's Id parameter which the page usually infers from the "URL"

Since you exactly replicated the URL, your page would have URL https://someinstance.salesforce.com/apex/mypage

change it to

https://someinstance.salesforce.com/apex/mypage?Id='someaccountIDthatalreadyexists"

Hope this helps you out :)

0

Its also a good idea to defend against not being able to locate the account matching the id, or if the id is missing entirely:

public MyController() {
   List<Account> accs = [SELECT Id, Name, Site FROM Account 
              WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
   if (accs.size()>0)
   {
       account=accs[0];
   }
   else
   {
      // do something sensible here - either create a new account or error
   }
}

You must log in to answer this question.

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