[SalesForce] How to use clear list when the list returns something that is used for other operations

I am querying account records and adding it to the list which in turn returns the values for another method called getSelected.I cleared the list just because its returning the previously selected values as shown

Partial code of page:

<apex:repeat value="{!alphabets}" var="a">
<apex:commandLink value="{!a}"  action="{!Alphabetorder}"  style="{!if($CurrentPage.parameters.alpha=a,'font-weight:bold','')}"  >
<apex:param name="alpha" value="{!a}"/>
</apex:commandLink>

&nbsp;|&nbsp;
 </apex:repeat>
            <apex:pageBlockSection Title="List of Available Accounts">

             <apex:dataTable value="{!accounts}" var="a" columnswidth="50px,50px"
                    cellpadding="4" border="1" styleClass="a-table">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox >
                                <apex:actionSupport event="onclick" action="{!GetSelected}"
                                    onsubmit="checkAll(this)" rerender="Selected_PBS" />
                            </apex:inputCheckbox>

Partial code of class:

  public PageReference Alphabetorder()
  {
  getAccounts();
  return null;
  } 
public List<accountwrapper> getAccounts()
{

 **accountList.clear();**
string s1;
   if(apexpages.currentpage().getparameters().get('alpha') == 'All')
       s1='%';
   else

       s1= apexpages.currentpage().getparameters().get('alpha')+'%';
       system.debug('alphabet ordering'+s1);

    for(Account acc: [Select a.Name, a.RecordTypeId, a.Phone, a.ParentId, a.Ownership, a.OwnerId, a.AccountNumber, (Select Id From Contacts), (Select Id From Opportunities)  From Account a where a.Name like :s1  ]) {
        System.debug('Account>>>>>>>>>>>>'+acc+acc.contacts.size());
        accountList.add(new accountwrapper(acc,acc.opportunities.size(),acc.contacts.size()));
    }
     //System.debug('account recordssss'+accountList);

    return accountList;

}

public PageReference getSelected()
{
    for(accountwrapper accwrapper: accountList)
    if(accwrapper.selected == true)
    selectedAccounts.add(accwrapper.acc);
                  System.debug('selected values'+selectedAccounts);

           return null;


}

Now since the return AccountList will become null it can't be used for getselected method? Where should I clear if that's the case?

Best Answer

On UI you are processing Accounts list and in controller you are referring accountlist. Use accountlist in VF page and you will get updated value.

Related Topic