2

I'm using following simple code to get list of Account via custom controller but it is not returning records.what I'm doing wrong ?

<apex:page controller="listcontroller" >
<apex:Form >
<apex:pagemessages ></apex:pagemessages>
   <apex:pageblock >
     <apex:pageblockTable value="{!acclist}" var="a">
        <apex:column >
         <apex:inputField value=" {!a.name}" />
       </apex:column>

     </apex:pageblockTable>
    </apex:pageblock>
  </apex:Form>
 </apex:page>

my apex code is :

public class listcontroller {
public List<Account> acclist{get;set;}
Public pagereference listcontroller()
{ 
acclist = [select id, name, industry from account limit 10 ];
return null;
}

}

I was reading this example in salesforce help https://www.salesforce.com/docs/developer/pages/Content/pages_custom_list_controller.htm

and it has made use of standardSet Controller, which seems little complex comparing to what I've written . is its(set controller) usage is necessary in case of custom controller to get list of records?

  • Looks like you were trying to make a constructor method, but by adding a return type and value, it has become a standard controller action. – Caspar Harmer Apr 26 '15 at 7:06
1

It looks like you were trying to make a constructor method, but by adding a return type and value, it has become a standard controller action. You can either remove the return type and value or make it a proper controller action (and rename it, please). Constructor:

Public listcontroller(){ 
    acclist = [select id, name, industry from account limit 10 ];
}

Or as a controller action:

Public PageReference getAccountList(){ 
    acclist = [select id, name, industry from account limit 10 ];
    return null;
}

Call this using @Prabhat's technique provided in his answer.

An alternative good technique is to explicitly get the list when you need it, in the getter of your account list. Try this:

public List<Account> acclist {
    get {
        if (acclist = null) {
            acclist = [select id, name, industry from account limit 10 ];
        }
        return acclist;
    }
    private set;
}

This is an example of a lazy accessor - meaning that when the apex:pageblockTable need the list, it calls the getter, ensuring that it will always have data in the list.

0

The reason its not returning any thing because you have used query in page reference and You need to invoke page reference method in your vf page.

Use action parameter in your apex:page tag something like this.

<apex:page controller="listcontroller"  action = {!listcontroller}>
  • This will call the constructor twice - probably not a good idea. – Caspar Harmer Apr 26 '15 at 3:53
  • @CaspNz -Public pagereference listcontroller() is not constructor its a method. Because constructor can not have return type. If you use return in constructor it will throw an error. The syntax for a constructor is similar to a method, but it differs from a method definition in that it never has an explicit return type and it is not inherited by the object created from it. – MIX DML Apr 26 '15 at 4:31
  • Thanks for the lesson Prabhat. Looks like you are right - anyway, Curious Techie should rename that method as it's extremely confusing. – Caspar Harmer Apr 26 '15 at 7:03
  • I actually think Curious Techie meant that to me a constructor, but added a return type by accident. The compiler would have informed him that he needed a return value, so he would have added it. – Caspar Harmer Apr 26 '15 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.