+ Start a Discussion
Daniel ButtsDaniel Butts 

Selected option from selectList not passed to Apex controller

I have created a page that displays opportunities across related accounts and I would like to allow users to create a new opportunity associated to one of those accounts directly from the page. Because I have a search function on the page as well, my form has multiple functions based on the action button used. If I simply added input fields for an Opportunity object, the field requirements (name, close date, etc.) would cause errors when searching. To resolve this I have created a custom Opportunity-like object (tOpp__c) without required fields and then I build a new Opportunity object in Apex. The problem I am running into is passing the selected option from a selectList into the controller so that I can populate fields on the Opportunity that require a lookup (Account, Owner, etc.) single-value input fields are passing data fine and the HTML output of the page appears to show the selected list populated correctly, but I get a null value in my debug logs for the selectList variable.

PAGE:
...
                 <apex:column headerValue="Account Name">
                     <apex:selectList size="1" value="{!tOpp.Account__c}">
                             <apex:selectOptions value="{!orgAcctOpts}"/>
                      </apex:selectList>
                 </apex:column>
...
                 <apex:column headerValue="">
                     <apex:commandButton value="Create" action="{!doCreateOpp}" rerender="all"/>
                 </apex:column>
...

CONTROLLER:

    public PageReference doCreateOpp() {
        System.Debug('Create Opportunity');
        for (tOpp__c tOpp : tOpps) {
            System.Debug('Starting Opportunity Insert: '+tOpp.Account__c);
            List<Account> accts = [select Id from Account where Name = :(tOpp.Account__c) limit 1];
            if (!accts.isEmpty()) {
                System.Debug('Account: '+accts.get(0));           
                Opportunity o = new Opportunity();
                o.Name = tOpp.Name;
                o.StageName = tOpp.StageName__c;
                o.CloseDate = System.today();
                o.Account = [select Id from Account where Name = :(tOpp.Account__c)];
                o.Owner = [select Id from User where Name = :(tOpp.Owner__c)];
                insert o;
            } else {
                System.Debug('Account Not Found: '+tOpp.Account__c);            
            }
        }
        return null;
    }

HTML:

...
       <td class=" dataCell  " id="summary_:form_:detail_:newopp_:0:j_id36" colspan="1">
            <select name="summary_:form_:detail_:newopp_:0:j_id37" size="1">    <option value="001m0000007HwqeAAC">Test1</option>
                <option value="001m0000007HwqyAAC">Test3</option>
                <option value="001m0000007HwrNAAS">Test5</option>
                <option value="001m0000007HwrDAAS">Test4</option>
                <option value="001m0000007HwqjAAC">Test2</option>
            </select></td>
...

DEBUG:

13:57:50.038 (38447582)|USER_DEBUG|[115]|DEBUG|Create Opportunities
13:57:50.038 (38455535)|SYSTEM_METHOD_EXIT|[115]|System.debug(ANY)
13:57:50.038 (38552803)|SYSTEM_METHOD_ENTRY|[116]|LIST<tOpp__c>.iterator()
13:57:50.038 (38745006)|SYSTEM_METHOD_EXIT|[116]|LIST<tOpp__c>.iterator()
13:57:50.038 (38777135)|SYSTEM_METHOD_ENTRY|[116]|system.ListIterator.hasNext()
13:57:50.038 (38792964)|SYSTEM_METHOD_EXIT|[116]|system.ListIterator.hasNext()
13:57:50.038 (38844991)|SYSTEM_METHOD_ENTRY|[117]|System.debug(ANY)
13:57:50.038 (38865250)|USER_DEBUG|[117]|DEBUG|tOpp.Account__c: null
13:57:50.038 (38872097)|SYSTEM_METHOD_EXIT|[117]|System.debug(ANY)
13:57:50.038 (38887525)|SYSTEM_METHOD_ENTRY|[118]|System.debug(ANY)
13:57:50.038 (38901300)|USER_DEBUG|[118]|DEBUG|Starting Opportunity Insert: null
13:57:50.038 (38907485)|SYSTEM_METHOD_EXIT|[118]|System.debug(ANY)
13:57:50.039 (39440025)|SOQL_EXECUTE_BEGIN|[119]|Aggregations:0|select Id from Account where Name = :tmpVar1 limit 1
13:57:50.042 (42866602)|SOQL_EXECUTE_END|[119]|Rows:0
13:57:50.042 (42983278)|SYSTEM_METHOD_ENTRY|[120]|LIST<Account>.isEmpty()
13:57:50.043 (43010277)|SYSTEM_METHOD_EXIT|[120]|LIST<Account>.isEmpty()
13:57:50.043 (43036387)|SYSTEM_METHOD_ENTRY|[131]|System.debug(ANY)
13:57:50.043 (43055532)|USER_DEBUG|[131]|DEBUG|Account Not Found:null
 
Best Answer chosen by Daniel Butts
Daniel ButtsDaniel Butts
I sorted this out. There were a couple of problems. First, I was populating some fields of the object in the controller which gave the appearance of some fields returning data from the page while others did not. Second, the tOpp property was missing the {get;set;} in its definition so the data wasn't coming back from the form. Rookie mistake ;)

All Answers

Balaji BondarBalaji Bondar
Hi Daniel,

You have not defined tOpp as property.Use code like below:
<apex:column headerValue="Account Name">
                     <apex:selectList size="1" value="{!selectedAccountId}">
                             <apex:selectOptions value="{!orgAcctOpts}"/>
                      </apex:selectList>
                 </apex:column>
...
                 <apex:column headerValue="">
                     <apex:commandButton value="Create" action="{!doCreateOpp}" rerender="all"/>
                 </apex:column>
Controller:

public selectedAccountId{get;set;}
public PageReference doCreateOpp() {

        System.Debug('Create Opportunity');
        for (tOpp__c tOpp : tOpps) {
            System.Debug('Starting Opportunity Insert: '+selectedAccountId);
            List<Account> accts = [select Id from Account where Name = :selectedAccountId limit 1];
            if (!accts.isEmpty()) {
                System.Debug('Account: '+accts.get(0));           
                Opportunity o = new Opportunity();
                o.Name = tOpp.Name;
                o.StageName = tOpp.StageName__c;
                o.CloseDate = System.today();
                o.Account = [select Id from Account where Id = :selectedAccountId];
                o.Owner = [select Id from User where Name = :(tOpp.Owner__c)];
                insert o;
            } else {
                System.Debug('Account Not Found: '+tOpp.Account__c);            
            }
        }
        return null;
    }
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
Yoganand GadekarYoganand Gadekar
here is an example that shows how to use picklist values in visualforc page:
http://www.cloudforce4u.com/2013/06/how-to-implement-custom-picklist-in.html
Daniel ButtsDaniel Butts
I sorted this out. There were a couple of problems. First, I was populating some fields of the object in the controller which gave the appearance of some fields returning data from the page while others did not. Second, the tOpp property was missing the {get;set;} in its definition so the data wasn't coming back from the form. Rookie mistake ;)
This was selected as the best answer