Reply
Trusted Contributor
BrianWK
Posts: 278

"Resetting" Select Options on Refresh

Scenario: This is building a list for a multi-insert.

 

I have a custom object called "Contract Products" that uses the Products object in Salesforce. The functionality I'm trying to create allows a user to populate fields for a Contract Product, Click a button and have it added to a list and then "reset" the entry fields to the defaults to repeat. I have most of this working, but I'm having difficulty with a select list.

 

I'm using a select list so only valid product are a selectable option (first step in process is to pick the pricebook). When a user selects a product, the controller uses the selected option to return the list price, unitprice and some other fields to "pre populate" the rest of the object. I have the list populating correctly and it works great - until I try to "reset."

 

What happens is the Product drop down retains the selected option. When I attempt to change the selection, the dropdown acts as a "read only" and reverts back to the option that was earlier selected.

 

 I thought I could "reset" by selecting the "-none-" label when I "add product" This does display "-none-" in the dropdown, but changing the product gives me an error. It errors on my "PopulateFields" method - the debug logs says that my "Product" is null.

 

/* Select Option Code */ private string product; public string getProduct() { return product; } public void setProduct(string Product) { this.product = product; } public list<SelectOption> getProducts() { list<PricebookEntry> SelectedPriceBookEntry = [Select p.UseStandardPrice, p.UnitPrice, p.ProductCode, p.Product2Id, p.Pricebook2Id, p.Name, p.IsActive, p.Id From PricebookEntry p where p.Pricebook2Id =:pricebook and p.IsActive = true order by p.Name]; list<SelectOption> pOptions = new list<SelectOption>(); pOptions.add(new SelectOption('','-None-')); for(PricebookEntry pbe : SelectedPriceBookEntry) { id pbeid = pbe.Product2id; string name = pbe.name; pOptions.add(new SelectOption(pbeId,Name)); } return pOptions; }

 

 

Code for populating object

 

private PricebookEntry PriceProduct; private Opportunity PriceBookOpp = new Opportunity(); public Opportunity GetPriceBookOpp() { return PriceBookOpp; } public void setPriceBookOpp(Opportunity PriceBookOpp) { this.PriceBookOpp = PriceBookopp; } public PageReference PopulateFields() { system.debug('This is the selected Product: ' + product); system.debug('This is the selected PriceBook: ' + getPricebook()); PriceProduct = [Select p.UnitPrice, p.ProductCode, p.Product2Id, p.Pricebook2Id, p.Name From PricebookEntry p where p.Product2Id = :product and p.Pricebook2Id = :pricebook]; system.debug('This is the selected PriceProduct: ' + PriceProduct.Name); NewCP.Contract_Product__c = PriceProduct.Product2ID; NewCP.Annual_List_Price__c = PriceProduct.UnitPrice; NewCP.Annual_Price__c = PriceProduct.UnitPrice; PriceBookOpp.amount = NewCP.Annual_Price__c * NewCP.Quantity__c; return null; }

 

Code to add product and "Reset"

 

public list<Contract_Product__c> Prods {get; set;} public void AddProduct() { system.debug('This is the new Contract Product: ' + NewCp); prods.add(NewCP); product = '-None-'; NewCP = new Contract_Product__c( Contract__c = Detail.Contract__c, Sale_Type__c = Detail.Sale_Type__c, Account__c = Detail.Account__c, Quantity__c = Detail.Quantity__c ); }

 

Part of the VF Page code

 

apex:form id="Pricebook" > <apex:pageblock id="SelectPricebook" title="Select Pricebook" > <apex:selectList value="{!Pricebook}" size="1" readonly="{!Display}" > <apex:actionSupport event="onchange" action="{!refresh}" rerender="SelectPricebook,SelectContractProducts" /> <apex:outputlabel value="Select Pricebook for all Contract Products: " style="font-weight:bold;" /> <apex:selectOptions value="{!items}" /> </apex:selectList> </apex:pageblock> <!-- Product selection list --> <apex:outputpanel id="SelectContractProducts" > <apex:pageblock id="ContractProductsBlock" title="Select Contract Products" rendered="{!Display}" > <apex:pageblockbuttons location="bottom"> <apex:CommandButton value="Add to Cart" action="{!AddProduct}" rerender="cart,SelectContractProducts,test,test2" rendered="{!NewCp.Annual_Price__c > 0}" status="status" /> <apex:CommandButton value="Refresh" rerender="cart,ProductCart" /> </apex:pageblockbuttons> <apex:pageblocksection columns="2" > <apex:pageblocksectionitem > <apex:outputlabel value="Account" /> <apex:inputfield value="{!NewCp.Account__c}" required="true" /> </apex:pageblocksectionitem> <!-- capped amount --> <apex:pageblocksectionitem > <apex:outputlabel value="Capped Amount" /> <apex:inputfield value="{!NewCp.Capped_Amount__c}" id="CappedAmount" /> </apex:pageblocksectionitem> <!-- Contract --> <apex:pageblocksectionitem > <apex:outputlabel value="Contract" /> <apex:inputfield value="{!NewCp.Contract__c}" /> </apex:pageblocksectionitem> <!-- Comments --> <apex:pageblocksectionitem > <apex:outputlabel value="Comments" /> <apex:inputfield value="{!NewCp.Comments__c}" /> </apex:pageblocksectionitem> <!-- Product --> <apex:pageblocksectionitem > <apex:outputlabel value="Product" /> <apex:selectList value="{!Product}" size="1"> <apex:actionSupport event="onchange" action="{!PopulateFields}" rerender="test,SelectContractProducts" /> <apex:selectOptions value="{!Products}" /> </apex:selectList> </apex:pageblocksectionitem> </apex:pageblocksection> </apex:pageblock>

</apex:form>

 

Trusted Contributor
BrianWK
Posts: 278

Re: "Resetting" Select Options on Refresh

Ok,

 

I just finished add a bunch of new debug code to my controller. This is what I've found is happening but I don't know why.

 

1. Product is returning as Null . I've confirmed that this is the case. While the select list remains populated after I call my "AddProduct" method, there's no longer IDs associated. If I click my new "Refresh" button that refreshes the whole page, my select list returns to "-None-" on it's own. Clicking on the select list still displays all the labels - but it doesn't appear any of these are associated with IDs anymore.

 

So what is happening to this select list? I assumed that when the panel is re-rendered  the select list would re-call my "Products" method in the selectOptions component. This doesn't seem to be the case. Using firebug, I can see that the select option is indeed populated the same. But yet  the getproduct and setproduct methods don't appear to return the ID value from the select list.

 

Anyone else have similar issues? Idea for me to try?