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
Swati.dhingraSwati.dhingra 

visualforce

hi
I m trying to make a custom controller, for that i am  creating a apex class.
For this i have taken the  following code from developer's guide, but i m getting an error .
 
public class MyController {

    public PageReference save() {
        return null;
    }

    public String getName() {
        return 'MyController';
    }

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


Error message :

Compile Error: Method does not exist or incorrect signature: ApexPages.currentPage() at line 13 column 30

  Help me to resolve the error.

Thanks in advance

Swati



Message Edited by Swati.dhingra on 06-10-2008 05:30 AM
Scott.MScott.M
There's a couple problems I think. Here's the example from the pages dev guide:


Code:
public class MyController {

private final Account account; public MyController() { account = [select id, name, site from Account where id = :System.currentPageReference().getParameters().get('id')]; }
public Account getAccount() { return account; }
public PageReference save() { update account; return null; }
}

The error your getting relates to this line
Code:
[select id, name from Account where id = :ApexPages.currentPage().getParameters().get('id')];


Which should be:

Code:
account = [select id, name, site from Account where id =
    :System.currentPageReference().getParameters().get('id')];

Also it doesn't look like you have a constructor defined which you should. Hope this helps.



Message Edited by Scott.M on 06-10-2008 08:49 AM
jwetzlerjwetzler
You are still on Spring 08 if ApexPages.currentPage() does not compile.  You can use System.currentPageReference() until you are upgraded (your page will still work but once you are upgraded it is recommended that you switch to using ApexPages.currentPage()).

You do not have to define a constructor unless you are using an Apex controller as an extension, or unless you want to execute any code in the constructor of your class.  A default constructor is implied.
Scott.MScott.M
:) wow I was way off,

Is there any reason you would want to load an object in the constructor like in the Summer 08 example and store it in a private final variable vs having the DML statement in the getAccount() function?


jwetzlerjwetzler
not sure that it matters either way.  If you're going to do it in your getAccount function though you should lazy load it.

Code:
Account acc;

public Account getAccount() {
        if (acc == null) {
           acc = [select id, name from Account 
                 where id = :ApexPages.currentPage().getParameters().get('id')]; 
        }
        return acc;
    }