You need to sign in to do that
Don't have an account?

Help with setting default Picklist value.
I have a controller which redirects a user to different visualforce pages depending on the picklist value selected.However, its not working exactly as expected. The 'Add to contact' option appears as the default value thus when i select on 'Add to Associate' , it opens the 'Addtocontact' page rather than opening 'Addtoassociate' page.
How do i set a default picklist value something like 'Select option to Add to' ,then get the 'Add to contact' & 'Add to Associate' working properly as expected.
Below is my controller and part of the visualforce page.
public class redirectonthebasisofpicklist { public list<selectoption>item{get;set;} public string picklistvalue{get;set;} public redirectonthebasisofpicklist() { item=new list<selectoption>(); item.add(new selectoption('Addtocontact','Add to contact')); item.add(new selectoption('AddtoAssociate','Add to Associate')); } public pagereference redirect() { PageReference pageRef= new PageReference('/apex/'+picklistvalue); pageRef.setredirect(true); return pageRef; } }
<apex:page controller="redirectonthebasisofpicklist" > <Apex:form > <apex:selectList value="{!picklistvalue}" size="1" > <Apex:selectOptions value="{!item}"/> <apex:actionSupport event="onchange" action="{!redirect}" /> </apex:selectList> </Apex:form> </apex:page>
You should just be able to add another option to the list:
and then check the user has selected one in the redirect method:
Hi Bob,
Thanks for your reply.However.this solution seems to work perfectly well with a simple scenario like the one above but i can't seem to make it work with the following piece of code: All options selected tend to return the error message part.
Note: If i don't iclude the return null; part at the end, i get this error: Save error: Non-void method might not return a value or might have statement after a return statement. on the bold line section.
If they all return the error message, that implies that the value isn't being set correctly in the controller. Can you post the VF?
Here is the whole of my visualforce page: The bold section is the one for handling selectList.
Hmm. You seem to have this selectlist embedded inside a pageblocktable, presumably as you have multiple records to display. However, every selectlist instance is backed by the same controller property - that means the last to write its information back to the controller will win. If you have a picklist per record, you'll need to use a wrapper class so that the value can be associated with the record.
Am not sure i understand what exactly you mean since i am already using a wrapper class. Let me give you the entire controller as well as a simplified visualforce page.
Note: I have also tried this without the wrapper class & it still behaved the same.
While you are using a wrapper class, the picklist section:
the value is set to {!picklistvalue} - this means that every selectlist on the page is backed by a single controller property. Thus when you submit the form (e.g. via the actionsupport) each selectlist writes its value to that one property.
Thanks for your assistance Bob,am not sure how i need to handle this.Could you please help me out on what the best solution for this should look like?
I'd suggest you add another attribute to the wrapper class and use that to back the selectlist.