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
Venkateshwarrao AdminVenkateshwarrao Admin 

Custom page


While savethepage I am getting error for "System.QueryException: List has no rows for assignment to SObject"
Code:
<apex:page controller="Mycontroller" tabStyle="Account">
<apex:form >
 <apex:pageBlock title="AccountDetails">
  <apex:pageBlockSection title="Accountinformation">
    <apex:inputField value="{!Account.Name}"/>
    <apex:inputField value="{!Account.Phone}"/>
    <apex:inputField value="{!Account.Fax}"/>
    <apex:inputField value="{!Account.ShippingCity}"/>
 </apex:pageBlockSection>
 <apex:pageBlockSection title="Account Detail">
   <apex:inputField value="{!Account.Annualrevenue}"/>
   <apex:inputField value="{!Account.Website}"/>
 </apex:pageBlockSection>
   <apex:pageBlockButtons >
   <apex:commandButton action="{!Save}" value="SavethePage"/>
  </apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

Class:
public with sharing class Mycontroller 
{
  Public Account acc;
  Public Mycontroller()
  {
  try{
     acc=[SELECT Id,Name,Phone,Fax,ShippingCity,Website,AnnualRevenue from account Where Id=:ApexPages.CurrentPage().getparameters().get('Id')];
         } 
    catch (Exception e) {
    acc= new Account();
       }  
  }
  public Account getaccount()
  {
  return acc;
  }
  Public Pagereference Save()
  {
  
    Update acc;
    Pagereference page=New ApexPages.standardController(acc).View();
    return page;
   }
}


Please any one provide me the solution. and tell me steps i have to follow
Abhishek BansalAbhishek Bansal
Hi,

Please change your Vf page and class code as follows :

VF :
<apex:page standardcontroller="Account" extensions="Mycontroller" tabStyle="Account">
<apex:form >
 <apex:pageBlock title="AccountDetails">
  <apex:pageBlockSection title="Accountinformation">
    <apex:inputField value="{!Account.Name}"/>
    <apex:inputField value="{!Account.Phone}"/>
    <apex:inputField value="{!Account.Fax}"/>
    <apex:inputField value="{!Account.ShippingCity}"/>
 </apex:pageBlockSection>
 <apex:pageBlockSection title="Account Detail">
   <apex:inputField value="{!Account.Annualrevenue}"/>
   <apex:inputField value="{!Account.Website}"/>
 </apex:pageBlockSection>
   <apex:pageBlockButtons >
   <apex:commandButton action="{!Save}" value="SavethePage"/>
  </apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Class :
public with sharing class Mycontroller 
{
  Public Account acc;
  public Mycontroller(ApexPages.StandardController stdController) {
  {
  acc = (Account)stdController.getRecord();
  try{
     acc=[SELECT Id,Name,Phone,Fax,ShippingCity,Website,AnnualRevenue from account Where Id=:acc.id];
         } 
    catch (Exception e) {
    acc= new Account();
       }  
  }
  public Account getaccount()
  {
  return acc;
  }
  Public Pagereference Save()
  {
  
    Update acc;
    Pagereference page=New ApexPages.standardController(acc).View();
    return page;
   }
}
Let me know if you have any issue.

Regards,
Abhishek

 
Vinoth Vijaya BaskerVinoth Vijaya Basker
Hi Venkateshwarrao, 

I was able to execute the code successfully. 

Please find the below steps that I have followed, 
  • Saved the code you posted as apex class and VF page
  • click preview button of VF page and add the parameter Id like this   ex: ?id=RecordIdOfAccount
  • Replace RecordIdOfAccount with your Accout record Id
Thanks,
Vinoth