+ Start a Discussion
Lindsay TackettLindsay Tackett 

Visualforce Selectlist Help

Hello All,

We are attempting to create a visualforce selectlist where our sales consultants can pick an approved lender (custom object Lender__c) for a custom object (Job__c).  These lenders have to be 'Approved' and be in the same 'Division' as the user.  I've gotten far enough as to where the list shows up on the page layout, and there is a save button but I cannot get the value they select from the dropdown to actually save.  

I think I'm okay up to the 'public PageReference save(){' but I am failing to figure out how to save the value the user selects.  Any help or direction is much appriciated.

Also, I'm terrible at all this visualfroce/apex stuff so if there are any suggestions on improving the code or better methods please let me know.  Thanks all!

VF Page:
<apex:page standardController="Job__c" extensions="selectoptions" tabStyle="Job__c">
<apex:form >
<apex:pageblock mode="edit">
<apex:outputLabel style="font-weight:Bold" value="Select a Lender :" />
<apex:SelectList size="1" value="{!lst}" multiselect="false">
<apex:selectOptions value="{!items}"/>
</apex:SelectList>
<br /><br />
<apex:pageBlockButtons location="bottom"> <apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>
</apex:pageblock>
</apex:form>
</apex:page>

Controller:

public with sharing class selectoptions
{
    public List<lender__c> lst
    {get;set;}
    public selectoptions(ApexPages.StandardController sc){}
    {
        lst = [select Name from Lender__c WHERE Lender_Status__c = 'Approved' AND My_Division_Lender__c = 1];
    }
    public List<SelectOption> getItems()
    {      
        List<SelectOption> options = new List<SelectOption>();
                   options.add(new SelectOption('','-Select-'));
        for(Integer i=0; i < lst.size(); i++)
        {
            options.add(new SelectOption(lst[i].Name,lst[i].Name));
        }
        return options;
    }
    public PageReference save(){
    update lst;
     return null;
    }
 }
Gaurish Gopal GoelGaurish Gopal Goel
Hi Lindsay,

You must be having a custom field for Lender on Job__c object. I am assuming that the field name is Lender__c. You can change the field name accordingly in the code on VF Page on Line#5. And you need to update JOB object not the Lender. I am sending you the updated code, it should work. Please let me know if it doesn't. 

If this answer solves your problem then mark it as the solution to help others. Thanks.

VF PAGE:
<apex:page standardController="Job__c" extensions="selectoptions" tabStyle="Job__c">
	<apex:form >
		<apex:pageblock mode="edit">
			<apex:outputLabel style="font-weight:Bold" value="Select a Lender :" />
			<apex:SelectList size="1" value="{!job.Lender__c}" multiselect="false">
				<apex:selectOptions value="{!items}"/>
			</apex:SelectList>
			<br /><br />
			<apex:pageBlockButtons location="bottom"> 
				<apex:commandButton value="Save" action="{!save}"/>
			</apex:pageBlockButtons>
		</apex:pageblock>
	</apex:form>
</apex:page>
CONTROLLER:
public with sharing class selectoptions
{
    public List<lender__c> lst{get;set;}
	public Job__c job{get;set;}
    public selectoptions(ApexPages.StandardController sc){}
    {
        this.job = (Job__c)stdController.getRecord();
		lst = [select Name from Lender__c WHERE Lender_Status__c = 'Approved' AND My_Division_Lender__c = 1];
    }
    public List<SelectOption> getItems()
    {      
        List<SelectOption> options = new List<SelectOption>();
	    options.add(new SelectOption('','-Select-'));
        for(Integer i=0; i < lst.size(); i++)
        {
            options.add(new SelectOption(lst[i].Name,lst[i].Name));
        }
        return options;
    }
    public PageReference save()
	{
		update job;
		return null;
    }
 }
Gaurish Gopal GoelGaurish Gopal Goel
Please change the Line#7 in the controller to
this.job = (Job__c)sc.getRecord();