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
VijoshVijosh 

Unable to call a method associated to a button when a SelectOption is included.

Hi,

I am new to apex and visualforce.I have written a simple code and in this code the when i click on the button the associated method is not called.
The code below is just a sample code with static assigments, the main problem is whenever I add a selectoption in my code the methods in the controllers do not work and I am unable to submit/commit the record.


<apex:page controller="test_controller">
    <apex:form >
        <apex:selectList value="{!countries}" multiselect="false" size="1">
           <apex:selectOptions value="{!Items}"></apex:selectOptions> 
        </apex:selectList><p/>
        <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
        
    </apex:form>
</apex:page>
public class test_controller
{
        public String[] countries = new String[]{};
                    
        public PageReference test() 
       {
             test__c t=new test__c();
             t.textfield__c='string';
             insert t;
        }
      
        public List<SelectOption> getItems() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('US','US'));
            options.add(new SelectOption('CANADA','Canada'));
            options.add(new SelectOption('MEXICO','Mexico'));
            return options;
        }
            
        public String[] getCountries() {
            return countries;
        }

}




Any suggestions would be helpful.

Regards,
Vijosh
Best Answer chosen by Vijosh
Arunkumar RArunkumar R
Hi Vijosh,

Can you use the below code, 

public class test_controller
{
      
 public string Countries{get;set;}
      
        public List<SelectOption> getItems() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('US','US'));
            options.add(new SelectOption('CANADA','Canada'));
            options.add(new SelectOption('MEXICO','Mexico'));
            return options;
        }
            
        public pageReference test() 
       {
             test__c t=new test__c();
             t.textfield__c='string';
             insert t;
             return null;
        }    
      
}
<apex:page controller="test_controller">
    <apex:form >
        <apex:selectList value="{!countries}" multiselect="false" size="1">
           <apex:selectOptions value="{!Items}"></apex:selectOptions> 
        </apex:selectList><p/>
        <apex:commandButton value="Test" action="{!test123}" rerender="out" status="status"/> 
    </apex:form>
</apex:page>



All Answers

Vinit_KumarVinit_Kumar
Change your code from :-

<apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>

to

<apex:commandButton value="Test" action="{!test}" />

I mean remove the reRender attribute from CommandButton.

If this helps,please mark it as best answer to help others :)
Arunkumar RArunkumar R
Hi Vijosh,

Can you use the below code, 

public class test_controller
{
      
 public string Countries{get;set;}
      
        public List<SelectOption> getItems() {
            List<SelectOption> options = new List<SelectOption>();
            options.add(new SelectOption('US','US'));
            options.add(new SelectOption('CANADA','Canada'));
            options.add(new SelectOption('MEXICO','Mexico'));
            return options;
        }
            
        public pageReference test() 
       {
             test__c t=new test__c();
             t.textfield__c='string';
             insert t;
             return null;
        }    
      
}
<apex:page controller="test_controller">
    <apex:form >
        <apex:selectList value="{!countries}" multiselect="false" size="1">
           <apex:selectOptions value="{!Items}"></apex:selectOptions> 
        </apex:selectList><p/>
        <apex:commandButton value="Test" action="{!test123}" rerender="out" status="status"/> 
    </apex:form>
</apex:page>



This was selected as the best answer
VijoshVijosh
Thanks Arun