Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an PageBlockTable which has a wrapper class as its value.

Every time a command button is clicked to add a product, a new row is added to the wrapper class and the PageblockTable is rerendered to display the new row. All these work but when the rerender happens the selectedValue in the selectList is reset to the first item on the selectList.

How can i have the selectList to maintain state?

    <apex:commandButton action="{!addNewProduct}" value="Add New Product" rerender="pbtExistingInstItems,PBCopyProdToInstItems"/>
    <apex:selectList id="slprodname" value="{!itw.installedItem.Product__c}" size="1" rendered="{!itw.isManuallyCreated ==  true}" >
                <apex:selectOptions value="{!prodList}"/>
                <apex:actionSupport event="onchange" action="{!updateDefaultProductValues}"  rerender="colInstallationName">


share|improve this question
Can you use sObjects picklist instead prodList? Is Product__c a picklist or a sting? – Sergey Utko Dec 17 '12 at 7:36
its not a picklist.. its lookup to product, but i just wanted a few products to be displayed which i am displaying from a custom setting – Prady Dec 17 '12 at 9:40
I think it is not possible to hold selected values after reRendering, unless you use sObject as container or holds values in an javascript array. – Sergey Utko Dec 17 '12 at 9:43
What do you think would be the best way to handle this scenario? I need to show in few product names for users to choose from, have to capture their IDs and place it in a field called product__c. – Prady Dec 17 '12 at 13:06
Do you have any sObject where products are stored as a picklist? – Sergey Utko Dec 17 '12 at 13:15
up vote 1 down vote accepted

Just want to explain my solution. Lets say you have some custom object "SomeObject" and a picklist field "ProductList__c" on that, where are all products collected. And you have a wrapper class. All you need is to add a copy of "SomeObject" to every row. And then at your page it could be like this:

<apex:commandButton action="{!addNewProduct}" value="Add New Product" rerender="pbtExistingInstItems,PBCopyProdToInstItems"/>

<apex:inputField value="{!itw.SomeObject.ProductList__c}" rendered="{!itw.isManuallyCreated}" id="slprodname">
    <apex:actionSupport event="onchange" action="{!updateDefaultProductValues}"  rerender="colInstallationName">

The picklist itw.SomeObject.ProductList__c will be rendered as a normal lelect list where selected value will be stored even if you re-renders the pageblock.

share|improve this answer

It depends on which kind of pagereference you return from your action method addNewProduct

If you are returning full URL then it loads the whole page, that's why it is reloading and reset to first item in select list.

So use only partial page load, return partial URL.

PageReference pr = new PageReference('/apex/PageName'); // for partial page load.

return pr; // use this

PageReference pr = new PageReference(URL.getSalesforceBaseUrl().toExternalForm()+'/apex/PageName'); // for full page load.

return pr; //don't use this.
share|improve this answer
i just do a rerender, no pageRef – Prady Dec 17 '12 at 6:33
Its the same page which gets refreshed and its done using rerender tag. So there is no need of any pageRefs in this instance – Prady Dec 17 '12 at 6:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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