Find your content:

Search form

You are here

Getting an error when trying to create a custom page with a custom controller

 
Share

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!


Attribution to: Di Zou

Possible Suggestion/Solution #1

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

Attribution to: jordan.baucke

Possible Suggestion/Solution #2

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 :)


Attribution to: Rao

Possible Suggestion/Solution #3

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

Attribution to: Bob Buzzard
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1597

My Block Status

My Block Content