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
VanitaKedarVanitaKedar 

List has no rows for assignment to SObject in Visualforce page

hello all,

i am working on 

following example of visual force custome controller
plz reff below link

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_custom.htm

and receved folling error

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


not sure whats wrong, and why it is not getting account data for user though there are multiple records in account for logged in user, who is developing the same vf page.

inputs are highly appreciated.

thank you,
vanita
 
VanitaKedarVanitaKedar
VF code

<apex:page controller="myController" tabStyle="Account">
    <apex:form>
        <apex:pageBlock title="Congratulations {!$User.FirstName}">
            You belong to Account Name: <apex:inputField value="{!account.name}"/>
            <apex:commandButton action="{!save}" value="save"/>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller code

public class MyController {

    private final Account myAccount;

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

    public Account getAccount() {
        return myAccount;
    }

    public PageReference save() {
        //Add your custom logic to update specific fields here 
        //e.g. myAccount.email = 'xx@xx.com'; 
        update myAccount;
        return null;
    }
}
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Vanitakedar,

You have to pass account id in the url.In the constructor(Mycontroller)method it is trying to fetch id from the page parameters.Since you ar not passing id in the url it is unable to fetch account record.Pass any account record id which you want to update
After clicking on preview pass account id in the url.
You url should be like this    http://yourinstance/apex/pagename?id=0012v00xxxxxxxx.

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards
VanitaKedarVanitaKedar
thanks but this is like work around.. this code is provided by Saleforce guide, i dont think so we should change it. we need to find whats root cause.. i guess may be some duplicate data causing issue..but yet not able to trace.. plz suggest more 
VanitaKedarVanitaKedar
any help around ?
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi vanitakedar,

This is not a work arround.The code itself is written in such a way where  the constructor(which is executed on page load) is querying account whose id is provided in the page URL.
SELECT Id, Name, Site FROM Account 
             WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
So if you dont pass id in the URL this ApexPages.currentPage().getParameters().get('id') will be null ,then we get query results as null.

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards