2

This is my VF page which has one Picklist Field and it is dynamic, I mean when a record is created in another object for ex:two__c, it's record name will get added to this picklist values.

Now the problem is when i map field from VF page to custom fields in order to save record in database, it is saving all fields except that Picklist value, here is my VF page and Custom Controller.

VF Page

<apex:page sidebar="false" standardController="check__c" extensions="accPickNamesDisplayClass1" tabStyle="check__c">
    <apex:form >
        <apex:pageBlock >

            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!record_insert}" />
            </apex:pageBlockButtons>

            <apex:pageBlockSection title="Information" >

                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Project</apex:outputLabel>
                    <apex:selectList multiselect="false" size="1">
                        <apex:selectOptions value="{!accNames}" />
                    </apex:selectList>
                </apex:pageBlockSectionItem>

                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Name</apex:outputLabel>
                    <apex:inputText value="{!Name}" />
                </apex:pageBlockSectionItem>

                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Email</apex:outputLabel>
                    <apex:inputText value="{!Email}" />
                </apex:pageBlockSectionItem>

                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Phone</apex:outputLabel>
                    <apex:inputText value="{!Phone}" />
                </apex:pageBlockSectionItem>

            </apex:pageBlockSection>     
        </apex:pageBlock>
    </apex:form>
</apex:page>

This is my controller

public class accPickNamesDisplayClass1 { 

    public String Phone    { get; set; }
    public String Email    { get; set; }
    public String Name     { get; set; }
    public String accNames { get; set; }

    public accPickNamesDisplayClass1(ApexPages.StandardController controller) {}

    public accPickNamesDisplayClass1() {}

    public PageReference record_insert () {
        check__c check = new check__c (
            Name__c = Name
            ,Email__c Email
            ,Mobile__c = Phone
            ,Project_Stage__c = accNames
        );

        insert check;
        check.clear();

        PageReference page = new PageReference('https://ap1.salesforce.com/a1t/o');
        page.setredirect(false);
        return page;
    }

    List<selectOption> options = new List<selectOption>();
    public List<selectOption> getAccNames() {
        for(two__c acc : [select Id,name from two__c])
        {
            options.add(new selectOption(acc.name,acc.name));
        }
        return options;
    }
}

1 Answer 1

4

You need to use value parameter of the apex:selectList:

A merge field that references the controller class variable that is associated with this selectList. For example, if the name of the associated variable in the controller class is myListSelections, use value="{!myListSelections}" to reference the variable. If multiselect is true, the value attribute must be of type String[] or a List of strings. Otherwise, it must be of type String.

Create a new variable for the selected value, for example selectedAccName:

public class accPickNamesDisplayClass1 { 
    public String selectedAccName {get; set;}

    public accPickNamesDisplayClass1() {
        selectedAccName = '';
    }

    public PageReference record_insert () {
        ...
        check.Project_Stage__c = selectedAccName;
    }
}

Visualforce:

<apex:selectList multiselect="false" size="1" value="{!selectedAccName}">
    <apex:selectOptions value="{!accNames}"/>
</apex:selectList>
1
  • Thank You for your response, I have another problem here, Actually i have override my both "new" and "edit" button with my VF page, but when i click the edit option on any record it is not retaining the field values which i had filled earlier when i was creating a record, how do i make it to retain field value.
    – Sharat
    Commented Feb 5, 2014 at 4:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .