I have a wrapper class to that include opportunity line items and a selectlist value set. When the page is rerendered, the oli fields are maintained, but the selectlist values are reset. I can't see why.
<apex:page standardController="Opportunity" extensions="OpportunityProductEntry" >
<apex:sectionHeader Title="Manage Products" subtitle="{!Opportunity.Name}"/>
<apex:messages style="color:red"/>
<apex:form id="frm">
<apex:outputPanel id="mainBody">
<apex:pageBlock title="Selected Products" id="sp">
<apex:variable var="rowNum" value="{!-1}" />
<apex:pageblockTable value="{!tableRows}" var="s" id="pgbt">
<apex:column >
<apex:commandLink value="Remove" action="{!RemoveFromTable}" reRender="sp" immediate="true" onComplete="ReRenderTable();">
<!-- this param is how we send an argument to the controller, so it knows which row we clicked 'remove' on -->
<apex:param value="{!s.oli.PriceBookEntryId}" assignTo="{!toUnselect}" name="toUnselect"/>
</apex:commandLink>
<apex:variable var="rowNum" value="{!rowNum + 1}" />
</apex:column>
<apex:column headerValue="Product Name" value="{!s.oli.PriceBookEntry.Product2.Name}"/>
<apex:column >
<apex:selectList value="{!s.pb}" size="1">
<apex:selectOptions value="{!s.pb}"/>
</apex:selectList>
</apex:column>
<apex:column >
<apex:inputText value="{!s.str}"/>
</apex:column>
<apex:column headerValue="Unit Price">
<apex:inputField value="{!s.oli.UnitPrice}" required="true" id="price_level"/>
</apex:column>
<apex:column headerValue="Quantity">
<apex:inputField value="{!s.oli.Quantity}" required="true"/>
</apex:column>
<apex:column headerValue="Discount Item">
<apex:inputField value="{!s.oli.netsuite_conn__Discount_Item__c}"/>
</apex:column>
</apex:pageblockTable>
<apex:pageBlockButtons >
<apex:commandButton action="{!OnCancel}" value="Cancel" immediate="true"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:outputPanel>
</apex:form>
</apex:page>
public with sharing class OpportunityProductEntry {
public Opportunity opp;
public opportunityLineItem[] shoppingCart {get;set;}
public String toUnselect {get; set;}
public List<aTableRow> tableRows { get; set; }
public List<selectoption> PriceBooks { get; set; }
public class aTableRow {
public OpportunityLineItem oli { get; set; }
public List<selectoption> pb { get; set; }
public String str { get; set; }
public aTableRow(OpportunityLineItem c, List<selectoption> a, String b) {
oli = c;
pb = a;
str = b;
}
}
public List<selectoption> getPriceBooks() {
List<selectoption> options = new List<selectoption>();
Schema.DescribeFieldResult fr = Product2.Family.getDescribe();
List<Schema.PicklistEntry> plelist = fr.getpicklistvalues();
for(Schema.Picklistentry ple: plelist){
options.add(new selectoption(ple.getvalue(),ple.getlabel()));
}
return options;
}
public OpportunityProductEntry(ApexPages.StandardController controller) {
opp = [SELECT Id FROM Opportunity WHERE Id = :controller.getRecord().Id];
shoppingCart = [
SELECT Id, netsuite_conn__Discount_Item__c, Quantity, UnitPrice, PriceBookEntryId, PriceBookEntry.Product2.Name
FROM OpportunityLineItem
WHERE OpportunityId = :opp.Id
];
PriceBooks = getPriceBooks();
tableRows = new List<aTableRow>();
for (OpportunityLineItem sc : shoppingCart) {
tableRows.add(new aTableRow(sc, PriceBooks, 'test'));
}
}
public PageReference RemoveFromTable() {
// This function runs when a user clicks "Remove" on an item in the "Selected Products" section.
Integer count = 0;
for (OpportunityLineItem d : shoppingCart) {
if ((String)d.PriceBookEntryId == toUnselect) {
tableRows.remove(count);
break;
}
count++;
}
return null;
}
public PageReference OnCancel() {
// If user hits cancel we commit no changes and return them to the Opportunity.
return new PageReference('/' + ApexPages.currentPage().getParameters().get('Id'));
}
}