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
Mahitha Vaka 9Mahitha Vaka 9 

Getting "Content cannot be displayed: List has no rows for assignment to SObject" on the custome object VF page?

Hi, I have created the below VF page and controller, but i am getting "Content cannot be displayed: List has no rows for assignment to SObject".
please advice on how to fix this.


<apex:page Controller="MyController" Tabstyle="Account">
<apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton action="{!Save}" value="Save"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!account.contacts}" var="contact">>
           
                            <apex:column headerValue="First Name"><apex:inputField value="{!contact.firstname}"/></apex:column>
                <apex:column headerValue="Last Name"><apex:inputField value="{!contact.lastname}"/></apex:column>
                <apex:column headerValue="Phone Number"><apex:inputField value="{!contact.phone}"/></apex:column>
                <apex:column headerValue="E-mail"><apex:inputField value="{!contact.email}"/></apex:column>
                <apex:column headerValue="Title"><apex:inputField value="{!contact.title}"/></apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>

    </apex:form>

</apex:page>


Controller:
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;
    }
}

              
Amit Chaudhary 8Amit Chaudhary 8
Please to update your code like below
public class MyController {

    private final Account account;

    public MyController() 
	{
		List<Account> lstAcc = [SELECT Id, Name, Site FROM Account 
                   WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
				   
		account = new Account();		   
		if(lstAcc.size() > 0 )
		{
			account = lstAcc[0];
		}		   
    }

    public Account getAccount() 
	{
        return account;
    }

    public PageReference save() 
	{
        update account;
        return null;
    }
}

NOTE:- The standard controller requires record context that can be provided by including a URL parameter named ‘id’ and assigned with a valid 15- or 18-digit record ID. The record specified by the ID must match the sObject type associated with the StandardController on the page, or an error will be thrown when the page attempts to render:
 
https://naX.salesforce.com/apex/CustomAccountPage?id=001A0000005HjZL



This is slightly different than the URL pattern to access a standard detail page for any record, which simply requires that the record id follow the Salesforce server instance name:
https://naX.salesforce.com/001A0000005HjZL



Let us know if this will help you