+ Start a Discussion
Rakesh SRakesh S 

Passing a custom picklist value from VF page to controller -- Getting Error

Hi All,

I have a custom picklist field that is Courses which has some drop down values like Salesforce,Java, SAP etc. and the same field in three tabs.
In 3rd Tab, when i select value like SAP then it goes to controller and did some actions.-- its working fine.

1st and 2nd Tab, when i select same value SAP then not passing any values to controller and every time it takes value like '---select course---'  in debug log.

And i observed one thing in Debug Log, Internally it takes value which i select from VF page and sets. Again the value is changed as ---Select Course---. 
My Page:
<apex:page standardController="Contact" extensions="RunningBatchController" id="pg">
    <apex:form id="frm">
        <apex:tabPanel switchType="client" selectedTab="name1" id="theTabPanel">
        <apex:tab label="Running Batch Information" name="name1" id="tabOne" title="Running Batch">
            <apex:pageBlock title="Running Batch ">
                <apex:pageBlockSection title="Batch Information" columns="1">
                <apex:selectlist value="{!selectedCourse}" size="1" id="rc" label="Course Name: "> 
                    <apex:SelectOptions value="{!CourseList}" />  
                  <!--  <apex:actionSupport event="onchange" id="rpbt"/> -->   
                </apex:selectlist>
        
            <apex:pageBlockSectionItem >
            <apex:outputLabel value="Batch No:"/>
            <apex:inputField value="{!contact.Batch_No__c}"/>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        <apex:pageBlockButtons >
            <apex:commandButton value="Search" action="{!searchBatches}" id="rbtn" reRender="rpbt"/>
        </apex:pageBlockButtons>
      <!--  <apex:pageBlockSection >-->
            <apex:pageBlockTable value="{!studentsList}" var="s" id="rpbt">
                <apex:column value="{!s.Name}"/>
                <apex:column value="{!s.Course__r.name}"/>
                <apex:column value="{!s.Batch_No__c}"/>
                <apex:column value="{!s.staff__c}"/>
                <apex:column value="{!s.MobilePhone}"/>
                <apex:column value="{!s.email}"/>
                <apex:column value="{!s.Status__c}"/>
            </apex:pageBlockTable>
     <!--   </apex:pageBlockSection>-->
    </apex:pageBlock>
    </apex:tab>
    
    
    <apex:tab label="Pending Batch Information" name="name2" id="tabTwo" title="Pending Batch">
            <apex:pageBlock title="Pending Batch ">
                <apex:pageBlockSection title="Batch Information" columns="1" id="pbs2">
                <apex:selectlist value="{!selectedCourse}" size="1" id="pc" label="Course Name: "> 
                    <apex:SelectOptions value="{!CourseList}"/>  
                    <apex:actionSupport event="onchange" reRender="ppbt"/>    
                </apex:selectlist>
                 
        </apex:pageBlockSection>
        <apex:pageBlockButtons >
            <apex:commandButton value="Search" action="{!searchPendingBatches}" id="btn" reRender="ppbt"/>
        </apex:pageBlockButtons>
      <!--  <apex:pageBlockSection >-->
            <apex:pageBlockTable value="{!studentsList}" var="s" id="ppbt">
                <apex:column value="{!s.Name}"/>
                <apex:column value="{!s.Course__r.name}"/>
                <apex:column value="{!s.Batch_No__c}"/>
                <apex:column value="{!s.staff__c}"/>
                <apex:column value="{!s.MobilePhone}"/>
                <apex:column value="{!s.email}"/>
                <apex:column value="{!s.Status__c}"/>
            </apex:pageBlockTable>
      <!--   </apex:pageBlockSection>-->
    </apex:pageBlock>
    </apex:tab>
    
    <apex:tab label="Completed Batch Information" name="name3" id="tabThree" title="Completed batch">
            <apex:pageBlock title="Completed Batch ">
                <apex:pageBlockSection title="Batch Information" columns="1" id="pbs3">
                <apex:selectlist value="{!selectedCourse}" size="1" id="cc" label="Course Name: ">
                     
                    <apex:SelectOptions value="{!CourseList}"/> 
                  <!--  <apex:actionSupport event="onchange" reRender="cpbt"/> -->   
                </apex:selectlist>
                 
        </apex:pageBlockSection>
        <apex:pageBlockButtons >
            <apex:commandButton value="Search" action="{!searchCompletedBatches}" id="Cbtn" reRender="cpbt"/>
        </apex:pageBlockButtons>
      <!--  <apex:pageBlockSection >-->
            <apex:pageBlockTable value="{!studentsList}" var="s" id="cpbt">
                <apex:column value="{!s.Name}"/>
                <apex:column value="{!s.Course__r.name}"/>
                <apex:column value="{!s.Batch_No__c}"/>
                <apex:column value="{!s.staff__c}"/>
                <apex:column value="{!s.MobilePhone}"/>
                <apex:column value="{!s.email}"/>
                <apex:column value="{!s.Status__c}"/>
            </apex:pageBlockTable>
      <!--   </apex:pageBlockSection>-->
    </apex:pageBlock>
    </apex:tab>
    
    </apex:tabPanel>
    </apex:form>
</apex:page>
​Controller:
public with sharing class RunningBatchController{
    
    public String selectedCourse { get; set; }
    public List<Contact> studentsList{get;set;}  
    
    public ApexPages.StandardController controller{get;set;}
    public RunningBatchController(ApexPages.StandardController controller) {
        this.controller = controller;
        system.debug('ddd '+controller);
        
    }

    public List<SelectOption> getCourseList(){
        
        List<SelectOption> CourseList = new List<SelectOption>();
        
        CourseList.add(new SelectOption('------- Select Course -------','------- Select Course -------')); 
        List<Product2> prodList = [Select id, Name from Product2];
        for(Product2 pc :prodList){
            CourseList.add(new SelectOption(pc.Id,pc.Name));     
        }
       return CourseList;    
    }
    
    
    
    
    
    public void searchBatches(){
        
        Contact bch = (Contact)controller.getRecord();
       system.debug('course '+selectedCourse);
       system.debug('batch '+bch.Batch_No__c);
      // String course = bch.Course__c;
       String batch = bch.Batch_No__c;
       
       if(selectedCourse != '------- Select Course -------' && batch != '--None--'){
           studentsList = [select id,Name,course__c,Course__r.name,Batch_No__c,Staff__c,MobilePhone,Status__c,Email from Contact where course__c =: selectedCourse and Batch_No__c =: batch and Status__c ='Active'];
        }else if(selectedCourse != '------- Select Course -------' && batch == '--None--'){
            studentsList = [select id,Name,course__c,Course__r.name,Batch_No__c,Staff__c,MobilePhone,Status__c,Email from Contact where course__c =: selectedCourse and Status__c ='Active'];
        }else{
            studentsList = [select id,Name,course__c,Course__r.name,Batch_No__c,Staff__c,MobilePhone,Status__c,Email from Contact where Status__c ='Active'];
        }
    }
    
    public void searchPendingBatches(){
        
        Contact bch = (Contact)controller.getRecord();
      system.debug('ppppp '+selectedCourse);
            
      if(selectedCourse == '------- Select Course -------'){
          system.debug('nothing ');
          studentsList = [select id,Name,course__c,Course__r.name,Batch_No__c,Staff__c,MobilePhone,Status__c,Email from Contact where Status__c ='Pending'];        
      }else{
          system.debug('selected course');
          studentsList = [select id,Name,course__c,Course__r.name,Batch_No__c,Staff__c,MobilePhone,Status__c,Email from Contact where course__c =: selectedCourse and Status__c ='Pending'];
        }
      
    }
    
    public void searchCompletedBatches(){
            
        Contact bch = (Contact)controller.getRecord();
                
       system.debug('cccc '+selectedCourse);
      if(selectedCourse == '------- Select Course -------'){
          system.debug('##');     
           studentsList = [select id,Name,course__c,Course__r.name,Batch_No__c,Staff__c,MobilePhone,Status__c,Email from Contact where Status__c ='In Active'];
           system.debug('@@'+studentsList.size());
      }else{
          system.debug('hii');
          studentsList = [select id,Name,course__c,Course__r.name,Batch_No__c,Staff__c,MobilePhone,Status__c,Email from Contact where course__c =: selectedCourse and Status__c ='In Active'];
        }
      
    }
    
}

 Debug Log:
User-added image

Please anyone help me with appropriate solution.

I appriciate your response.

Thank You
Rakesh.
Best Answer chosen by Rakesh S
Krishna SambarajuKrishna Sambaraju
Hi Rakesh,

You have declared only one variable "selectedCourse" and you are using it on three tabs. As your third tab is the last one in the visualforce page, it is taking the value from that list.
So declare a separate variable for each of the tabs as below.
private string selectedCourse;
public string selectedCourse1 {get; set}
public string selectedCourse2 {get; set;}
public string selectedCourse3 {get; set;}

 public List<SelectOption> getCourseList(){
        
        List<SelectOption> CourseList = new List<SelectOption>();
        
        CourseList.add(new SelectOption('','------- Select Course -------')); 
        List<Product2> prodList = [Select id, Name from Product2];
        for(Product2 pc :prodList){
            CourseList.add(new SelectOption(pc.Id,pc.Name));     
        }
       return CourseList;    
    }
    
    
    
    
    
    public void searchBatches(){
        
        Contact bch = (Contact)controller.getRecord();
		if (selectedCourse1 != '')
		{
			selectedCourse = selectedCourse1;
		}
		if (selectedCourse2 != '')
		{
			selectedCourse = selectedCourse2;
		}
		if (selectedCourse3 != '')
		{
			selectedCourse = selectedCourse3;
		}
       system.debug('course '+selectedCourse);
       system.debug('batch '+bch.Batch_No__c);
      // String course = bch.Course__c;
       String batch = bch.Batch_No__c;
       
       if(selectedCourse != '------- Select Course -------' && batch != '--None--'){
           studentsList = [select id,Name,course__c,Course__r.name,Batch_No__c,Staff__c,MobilePhone,Status__c,Email from Contact where course__c =: selectedCourse and Batch_No__c =: batch and Status__c ='Active'];
        }else if(selectedCourse != '------- Select Course -------' && batch == '--None--'){
            studentsList = [select id,Name,course__c,Course__r.name,Batch_No__c,Staff__c,MobilePhone,Status__c,Email from Contact where course__c =: selectedCourse and Status__c ='Active'];
        }else{
            studentsList = [select id,Name,course__c,Course__r.name,Batch_No__c,Staff__c,MobilePhone,Status__c,Email from Contact where Status__c ='Active'];
        }
    }
and use the corresponding variable in the select list in each of the tabs (for tab1 use selectedCourse1 and so on). Let me know if you still have any problems.

Regards,
Krishna.