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
FrancoFranco 

Selecting Accounts with inputcheckbox

I'm trying to create a page where a list of all accounts are presented to the user so he/she can select accounts to be displayed on the bottom part of the page.  I'm trying to display inputcheckboxes next to each account to to capture the user's request.   For some reason, I can't get the initial list of accounts to display.

My code is below -

Code:
<apex:page Controller="AccountSelectController">
  <apex:form >  
   <apex:pageBlock title="Available Accounts">
      <apex:pageBlockTable title="Accounts" id="PickAccts" value="{!MyAcct}" var="aAcct" >
        <apex:column>
          <apex:inputCheckBox value="{!aAcct.selected}" Id="selectLine"/>
        </apex:column>
        <apex:column headerValue="Account Id" value="{!aAcct.theacct.Id}" />
        <apex:column headerValue="Account Name" value="{!aAcct.theacct.Name}" />
      </apex:PageblockTable>
    <apex:commandButton action="{!gensAcct}" value="Select" rerender="MyPanel" status="status"/> <br />
    <apex:pageMessages ></apex:pageMessages>
  </apex:pageBlock>

<apex:actionStatus startText="Updating..." id="status"/>

<apex:outputpanel title="Results" id="MyPanel">
  <apex:outputpanel title="Wrapper" id="Wrapper" rendered="{!NOT(ISNULL(SAcct))}">
    <apex:pageBlock >
      <apex:pageBlockTable title="Accounts" id="PickAccts" value="{!SAcct}" var="aAcct" >
        <apex:column headerValue="Account Id" value="{!aAcct.theacct.Id}" />
        <apex:column headerValue="Account Name" value="{!aAcct.theacct.Name}" />
      </apex:PageblockTable>
    </apex:Pageblock>
  </apex:outputpanel>
</apex:outputpanel>

  </apex:form>
</apex:page>

Controller -
Code:
public class AccountSelectController {

    public PageReference genSAcct() {
        SAcct = getSAcct();
        return null;
    }
    
    public class cAcct{
        public Account theacct {get; set;}
        public Boolean selected{get; set;}
       
        public cAcct(Account a,Boolean s){
            theacct = a;
            selected = s;
        }
    }
    
    //A collection of the class/wrapper objects cAcct
    
    //The fist list includes all Accounts
    public List<cAcct> MyAcct {get; set;}
    
    //The second list only inclues Accouts selected
    public List<cAcct> SAcct {get; set;}

   //This method uses a SOQL query to return a list of all Accounts
    public List<cAcct> getMyAcct(){
          if(MyAcct == null){
              MyAcct = new List<cAcct>();
              for(Account acct : [Select Name FROM Account Limit 10  ]){
                    /* As each opportunity is processed I create a new cOpportunity object and add it to MyList */
                    cAcct myadd = new cAcct(acct,FALSE);
                    MyAcct.add(myadd);
              }
          }
        return MyAcct;
    }
    
    //This method uses looks at the full account list and adds only those with "selected" = true
    public List<cAcct> getSAcct(){
      if (MyAcct != null) {  
          if (SAcct == null) {
              SAcct = new List<cAcct>();
              for(cAcct cacct : MyAcct){
                  /* As each Accout is processed, check if the selected value is true. */
                  if (cacct.selected == TRUE) {
                      SAcct.add(cacct);
                  }
              }
          }
          return SAcct;
      } else {
          SAcct = null;
          return SAcct;
      }
    }
}

 

 

TehNrdTehNrd
Change:
Code:
//The fist list includes all Accounts
public List<cAcct> MyAcct {get; set;}

To:
Code:
//The fist list includes all Accounts
List<cAcct> MyAcct;

What you have done is basically defined two get methods and the get method which query the account is never being executed.

I have found that it is best to use apex properties {get; set;} for primitive variables such as strings, booleans, numbers etc.
 



Message Edited by TehNrd on 01-19-2009 09:01 AM
FrancoFranco

Perfect!  Worked like a charm.

Franco

Toasty02Toasty02

Question...how is an account marked as selected when the user clicks the checkbox, i.e., where does the aAcct.selected property get set to true?  I am trying to do something similar but cannot get it to work.

 

Thanks!

- Carolyn

UmapenUmapen
I am doing the same. I want to add a WHERE clause to the query from user input.

My Apex page


Code:

<apex:pageBlockSection><apex:outputLabel for="BillingPostalCode">Search Text<apex:outputLabel>
<apex:panelGroup >
<apex:inputText id="subMname" value="{!subMname}"/>
<apex:commandButton value=" Go!" action="" rerender="" status="status2"/>
</apex:panelGroup>
</apex:pageBlockSection>
Code:
//getter and setter for submatket name - step 1
public String subMname { get { return subMname ;} set { subMname=value ; } }
.......
// Modified
//This method uses a SOQL query to return a list of all Accounts
public List getMyAcct(){
String queryName = '%' + subMname + '%'; // this is empty System.debug('query filter queryName' + queryName + 'subMname' + subMname );
if(MyAcct == null){
MyAcct = new List();
for(Account acct : [Select Name, BillingCity, OwnerId FROM Account WHERE Name LIKE :queryName LIMIT 12]){
/* As each opportunity is processed I create a new cOpportunity object and add it to MyList */
cAcct myadd = new cAcct(acct,FALSE);
MyAcct.add(myadd);
}
}
System.debug('Exiting getMyAcc() ......');
return MyAcct;
}

 

But I do not know why value is coming as null in what is that I am doing wrong: