0

while creating the account record am getting this error at Class.MyController.: line 5, column 1 please help me to resolve

 <apex:page STAndardController="account" extensions="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

public class MyController
{
     public  Account account{get; set;} 
     public MyController(ApexPages.StandardController controller) 
     {
         account=[SELECT ID,Name, site FROM Account 
                   WHERE Id=:Apexpages.currentPage().getParameters().get('id')]; 
     } 

     public MyController()
     {
           account=[SELECT ID,Name, site FROM Account 
                     WHERE Id=:Apexpages.currentPage().getParameters().get('id')];
     } 
     public Account getAccount()
     {
        return  account ;
     }
     public  void save()
     {
        update account;
     } 
}
1
  • Not really answering the question, but still. You don't have to query for the record based on a GET parameter. If you create a page with a standard controller and an extension, the constructor of the extension gets the standard controller as a parameter. You can use that to reference the record that is loaded. Check the documentation on the standard controller: developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/… Commented Apr 29, 2016 at 6:48

3 Answers 3

3

from security perspective you must always validate url params. Also standard controller has useful method getId();

Here it is a small example:

public with sharing class AccountController {

public  Account account{ get; set; }

public AccountController(ApexPages.StandardController controller) {
    Id accId = controller.getId();
    if (accId == null) {
        return;
    }
    this.account = [SELECT ID, Name FROM Account WHERE Id =: accId];
}

public AccountController() {
    String tmpId = Apexpages.currentPage().getParameters().get('id');
    System.debug(tmpId);
    if (String.isBlank(tmpId)) {
        return;
    }
    Id accId;
    try {
        accId = Id.valueOf(tmpId);
    }
    catch (Exception e) {
        return;
    }
    List<Account> accs = [SELECT ID, Name FROM Account WHERE Id =: accId LIMIT 1];

    if (!accs.isEmpty()) {
        this.account = accs[0];
    }
}

}

2
  • 1
    Since you're using the standard controller getId() method, why add a query when you can use this.account = (Account)controller.getRecord();. Are there pros/cons to using this? Commented Apr 29, 2016 at 14:24
  • 1
    I rarely user the getRecord method because of this Note that only the fields that are referenced in the associated Visualforce markup are available for querying on this SObject. All other fields, including fields from any related objects, must be queried using a SOQL expression. Commented May 2, 2016 at 7:03
1

Following query is causing the issue:

account=[SELECT ID,Name, site FROM Account WHERE    Id=:Apexpages.currentPage().getParameters().get('id')];

Either you make sure you have passed correct id in Id parameter or use List<Account> instead of account instance variable.

4
  • thank u ashwin.. I tried but still same error showing.
    – user30881
    Commented Apr 29, 2016 at 7:22
  • can you post the error message @user30881
    – Ashwani
    Commented Apr 29, 2016 at 7:23
  • System.QueryException: List has no rows for assignment to SObject Class.MyController.<init>: line 5, column 1.
    – user30881
    Commented Apr 29, 2016 at 7:24
  • 1
    As i wrote in answer, you need to make changes according to it. @user30881
    – Ashwani
    Commented Apr 29, 2016 at 7:29
0

An alternative solution:

public with sharing AccountController {
  private ApexPages.StandardController ctlr;

  public Account account {
    get {
      try {
           this.account = this.ctlr == null
                        ? [select id, ... from Account where id = : Apexpages.currentPage().getParameters().get('id') limit 1]
                        : (Account) this.ctlr.getRecord();
      }
      catch (QueryException e) {ApexPages.addMessage(...); this.account = new Account();}
      return this.account;
    }
    set;
  }

  public AccountController(ApexPages.StandardController ctlr) {this.ctlr = ctlr;}

  public AccountController() {}
}

It is not clear to me why you have two constructors for this - one as a controller extension and one as a custom controller but I'll assume that is the intent.

The above alternative solution lets the getters do the work rather than constructors, where I tend to like short and simple ones.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .