function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Felix HerzogFelix Herzog 

List has no rows for assignment to SObject. Visualforce Developer Guide

Hello erveryone,

I´m currently learning about the Wizard development with Visual force.
For this i´m using the Visualforce Developer Guide. But i´m stuck at this session: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_controller_getter_methods.htm
This is the error i get: System.QueryException: List has no rows for assignment to SObject
Class.MyController.getAccount: line 8, column 1

Apperently this means, that the querry in my controller returned nothing but i don´t know why since my code and makrup is identicle with the guid.

This ist the markup:
<apex:page controller="MyController" tabStyle="Account">
  <apex:pageBlock title="Hello {!$User.FirstName}!">
      This is your new page for the {!name} controller. <br/>
      You are viewing the {!account.name} account.
  </apex:pageBlock>
</apex:page>

This is the controller:
public class MyController {
    
    public string getName() {
        return 'MyController';
    }
    
    public Account getAccount() {
         return [select id, name from Account 
                 where id = :ApexPages.currentPage().getParameters().get('id')];
   }
}
Best Answer chosen by Felix Herzog
rafaelburirafaelburi

As the article (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_controller_getter_methods.htm) mentions 

You can access your VF thru the following URL, this URL provides the account Id as parameter. You can replace the Id value with any valid Account Id

https://Salesforce_instance/apex/MyFirstPage?id=001D000000IRt53

Thus, your code in controller can get that parameter value thru the highlighted code below.

return [select id, name from Account where id = :ApexPages.currentPage().getParameters().get('id')];

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Felix,

You need to add this visual force page in the Account page. If you are just prevewing you will get an error because the below parameter will be null.

:ApexPages.currentPage().getParameters().get('id')].

Navigate to setup-> visualforce pages-> Select the visualforce page and click on edit button and check the checkbox as shown below.
User-added image
Once this is checked you can add this to the lightneing page and you will get the desired result as shown below.
User-added image
Please let me know if you need any further information on it.

If this solution helps, Please mark it as best answer.

Thanks,
 
mickbiden mickbidenmickbiden mickbiden
thank you so much for taking the time to share a wonderful article.
 
Felix HerzogFelix Herzog
Hello Sai,

thank you fpr the help but i´m still not sure what I need to do.
I added the mark for the availability in the lightning experience and also createt a custom tab with the visual-force page in the lightning experience. But I still get the same error. How exactly do I add the page in the Account-Object?
rafaelburirafaelburi

As the article (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_controller_getter_methods.htm) mentions 

You can access your VF thru the following URL, this URL provides the account Id as parameter. You can replace the Id value with any valid Account Id

https://Salesforce_instance/apex/MyFirstPage?id=001D000000IRt53

Thus, your code in controller can get that parameter value thru the highlighted code below.

return [select id, name from Account where id = :ApexPages.currentPage().getParameters().get('id')];

This was selected as the best answer