function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Behzad Bahadori 18Behzad Bahadori 18 

Passing the Selected option to VisulaForce Page

I have the following drop down, and I want to send the value of the selected file to my controller .. so if the user selects parent account1 my controller should get that value or if they select parent account2 samething 
<label>Select the Region</label>
                </td>
                <td class="data2Col" style="border-bottom:0px;">
                    <select id="sandler" class="sandler" value="{!regionAccount}">
            <option value=""></option>
            <option value='ParentAccount1'>asdf--zz</option>
            <option value='ParentAccount2'>ggggg</option>
        
        </select>
        </td>
// Recieve the Region Value from Visual Force Page.. 
   public String regionAccount{get;set;}
    public Account regionAccountId{get;set;}
// if regional is not empty 
if (regionAccount != Null ){


      // get the value of region and find the accountId
        regionAccountId = [select Id  from Account where Name = : regionAccount];
        account.ParentId =regionAccountId.Id;
    
 
        }
        else{
        for (Contact parents : parentAccount)
        {
            System.debug('Now I get to here part 2');
            account.ParentId = parents.AccountId;
        }
    }


 
Best Answer chosen by Behzad Bahadori 18
hitesh90hitesh90
Hello Behzad,

You have to use "<apex:selectList>" instead of <select>. see below example.

Visualforce Page:
<apex:page controller="regionAccountController">
    <apex:form>
        <apex:selectList id="chooseColor" value="{!regionAccount}" size="1">
            <apex:selectOption itemValue="" itemLabel="-- None --"/>
            <apex:selectOption itemValue="ParentAccount1" itemLabel="asdf--zz"/>
            <apex:selectOption itemValue="ParentAccount2" itemLabel="ggggg"/>           
        </apex:selectList>
        <br/>
        <apex:commandButton value="Regional Account" action="{!regionalAccProcess}"/>
    </apex:form>
</apex:page>

Apex Class:
public class regionAccountController{
    public String regionAccount{get;set;}
    public regionAccountController(){
    }    
    public void regionalAccProcess(){
        system.debug('Select Option Value'+regionAccount);        
        // if regional is not empty
        if (regionAccount != Null ){
            List<Account> regionAccList = [select Id, Name from Account where Name = : regionAccount];
            system.debug('Selected Account is '+regionAccList);
        }
    }
}

Let me know if you have any question on this. Please mark this "Solved" if it helps.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
 

All Answers

hitesh90hitesh90
Hello Behzad,

You have to use "<apex:selectList>" instead of <select>. see below example.

Visualforce Page:
<apex:page controller="regionAccountController">
    <apex:form>
        <apex:selectList id="chooseColor" value="{!regionAccount}" size="1">
            <apex:selectOption itemValue="" itemLabel="-- None --"/>
            <apex:selectOption itemValue="ParentAccount1" itemLabel="asdf--zz"/>
            <apex:selectOption itemValue="ParentAccount2" itemLabel="ggggg"/>           
        </apex:selectList>
        <br/>
        <apex:commandButton value="Regional Account" action="{!regionalAccProcess}"/>
    </apex:form>
</apex:page>

Apex Class:
public class regionAccountController{
    public String regionAccount{get;set;}
    public regionAccountController(){
    }    
    public void regionalAccProcess(){
        system.debug('Select Option Value'+regionAccount);        
        // if regional is not empty
        if (regionAccount != Null ){
            List<Account> regionAccList = [select Id, Name from Account where Name = : regionAccount];
            system.debug('Selected Account is '+regionAccList);
        }
    }
}

Let me know if you have any question on this. Please mark this "Solved" if it helps.

Thank You,
Hitesh Patel
Email :- hiteshpatel.aspl@gmail.com
http://mrjavascript.blogspot.in/
 
This was selected as the best answer
Behzad Bahadori 18Behzad Bahadori 18
HI thank you for respond I get the following error Error: Unsupported attribute class in <apex:selectList>
Behzad Bahadori 18Behzad Bahadori 18
never mind I had class in there that is why
hitesh90hitesh90
Alright Behzad.. let me know if you face any issue in this.
Behzad Bahadori 18Behzad Bahadori 18
Compile Error: Initial term of field expression must be a concrete SObject: List<Account> 
Behzad Bahadori 18Behzad Bahadori 18
my controller is compllaining about that List<account> now
Behzad Bahadori 18Behzad Bahadori 18
I made a for loop to go through the AccountId is
Behzad Bahadori 18Behzad Bahadori 18
Is there a chance you can help me out with the Unit test on this?