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
PallavPallav 

Value of picklist not persisting on page refresh, any idea why??

Hi All,

I am facing a issue with the SelectList, if I populate the SelectList with the name of an Account the select from and that is along with some more fields are mendetory. Then if I select the Account name from the SelectList and did not provide any input to any one of the the required fields. Then the page returns back itself and all other values along with the static picklists which are hard-coded their values remains unchanged except the value of the picklist that is feeded with the Account names in the salesforce instance.

Given below is the code for the page and controller:

<apex:selectList id="fcHotel" value="{!Accounts}" style="width: 140px" styleClass="std" size="1">
                <apex:selectOptions value="{!accList}"/>
              </apex:selectList>

Controller Section:

Map<String,String> accMap= new Map<String,String>();

public String getAccounts()
    {
        return accMap.get(selectedacc);
    }
    public void setAccounts(String selectedacc)
    {
        this.selectedacc= selectedacc;
    }

public List<SelectOption> getaccList()
{   
    List<SelectOption> options = new List<SelectOption>();
        Account[] accounts = [Select Id,Name from Account];
       
                options.add(new SelectOption('','-- Select One --'));
               
            for(Acocunt account : accounts )
            {   
                accMap.put(account .Id,account .Name);
                options.add(new SelectOption(account .Id,account .Name));
            }
         return options;           
}


Thanks in anticipation of your response.

Thanks and regards
Pallav
Ron HessRon Hess
you have written a getter, but i do not see a setter in your code.

with a setter  --> setAccList( string val ) , you controller code can know what was selected.

then you must store this in the controller , perhaps in a string class var, then when your page is rerendered, if this class var is not null, make sure to use it to set the selected item in the list  ( in the getAccList () )

This should allow you to preserve the current selection between page rerender.  it will not work if you refresh the entire page as this clears out the view-state.