+ Start a Discussion
nikita dhamalnikita dhamal 

how to get selected picklist value in controller

My selected value is stored in the variable 'val' and in the controller  i have given {get;set;} for val but val is shoing null instead of selected value from the picklist..
 <apex:selectList id="selected_list" value="{!val}" required="false" size="1">
       <apex:selectOption itemvalue="AllTime" itemLabel="AllTime"/>
          <apex:selectOption itemvalue="CurrentWeek" itemLabel="CurrentWeek"/>
          <apex:selectOption itemvalue="LastWeek" itemLabel="LastWeek"/>
       <apex:actionSupport event="onchange" action="{!viewData}" reRender="block"/>
          </apex:selectList>

Controller:

public string val{get;set;}
 public pageReference viewData(){
 getTimesheetEntries();
 return null;
 }
public List<Time_Log__c>getTimesheetEntries()
{
   system.debug('****************************Value*****************************'+val); 
    List<Time_Log__c> timesheetentries = new List<Time_Log__c>();
    
    if(val =='CurrentWeek')
    {
    system.debug('****************************+++++++++INSIDE if Current Week+++++++*****************************'); 
    timesheetentries = [select id,name,Allocation__c,Assigned_to__c,Action_Status__c,End_Date__c,Billable__c,Start_Date__c,Milestone__c,Progress_Percentage__c,Role__c,Projects__c ,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c where LastModifiedDate = THIS_WEEK];
    
    }
    if(val =='LastWeek')
    {
    system.debug('****************************+++++++++INSIDE if LAst Week+++++++*****************************'); 
    timesheetentries = [select id,name,Allocation__c,Assigned_to__c,Action_Status__c,End_Date__c,Billable__c,Start_Date__c,Milestone__c,Progress_Percentage__c,Role__c,Projects__c ,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c where LastModifiedDate = LAST_WEEK];
   
    }
    else
    { 
     system.debug('****************************+++++++++INSIDE else+++++++*****************************');    
     timesheetentries = [select id,name,Allocation__c,Assigned_to__c,Action_Status__c,End_Date__c,Billable__c,Start_Date__c,Milestone__c,Progress_Percentage__c,Role__c,Projects__c ,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c];
    }
    return timesheetentries ;
 }
 
 
Best Answer chosen by nikita dhamal
Srinivas SSrinivas S
Hi Nikita,

I guess issue is with the form. If you are not populating the required fields in the form and try to change the 'Week' picklist which is invoking an apex method, it will try to send the entire visualforce page to server to process. When it send since few mandatory fields we are not filling up functionality is not working as expected.

To avoid the above issue, we should wrap the selectlist inside of action region. It will resolve the issue.
I have tested, it is working for me.

Controller Class:
public with sharing class onPickSelDisRecords1 {
    public String val {get;set;}
    public Time_Log__c tsObj{get;set;}
    public boolean checkbx{get;set;}
    public boolean flag= false;
    public Decimal d;

    public onPickSelDisRecords1() {
    system.debug('***************************************'+val);
        val = 'Current Week';
        tsObj = new Time_Log__c();
    }
    
    public PageReference cancel(){   
        PageReference page = new PageReference('/apex/Demo');
        page.setRedirect(true);
        return page;
    }
    public PageReference save() {
        PageReference page = new PageReference('/apex/Demo');
        List<Time_Log__c> timesheetentries = new List<Time_Log__c>();
        timesheetentries = [select id,name,Allocation__c,Projects__c ,Assigned_to__c,End_Date__c,Action_Status__c,Billable__c,Start_Date__c,Milestone__c,Progress_Percentage__c,Role__c,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c ];

        for(Time_Log__c ts: timesheetentries){
            if(ts.Projects__c==tsObj.Projects__c && ts.Task_If_Assigned__c==tsObj.Task_If_Assigned__c) {
                d=tsObj.Time_Spent_Hrs__c;
                flag=true;
            }
        }
        if(flag == true) {
            tsObj=[ select id,name,Allocation__c,Assigned_to__c,Projects__c ,Action_Status__c,Start_Date__c,End_Date__c,Billable__c,Milestone__c,Progress_Percentage__c,Role__c,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c where Projects__c=:tsObj.Projects__c and Task_If_Assigned__c=:tsObj.Task_If_Assigned__c];
            tsObj.Time_Spent_Hrs__c= tsObj.Time_Spent_Hrs__c+d;
            update tsObj;
            page.setRedirect(true);
        }
        else {
            insert tsObj;
            page.setRedirect(true);
        }
        return page;
    }
    
    public List<Time_Log__c > getAccounts() {
        if(val == 'CurrentWeek')
            return [select id,name,Allocation__c,Assigned_to__c,Action_Status__c,End_Date__c,Billable__c,Start_Date__c,Milestone__c,Progress_Percentage__c,Role__c,Projects__c ,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c where LastModifiedDate = THIS_WEEK];
        else if(val == 'LastWeek')
            return [select id,name,Allocation__c,Assigned_to__c,Action_Status__c,End_Date__c,Billable__c,Start_Date__c,Milestone__c,Progress_Percentage__c,Role__c,Projects__c ,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c where LastModifiedDate = LAST_WEEK];
        else
            return [select id,name,Allocation__c,Assigned_to__c,Action_Status__c,End_Date__c,Billable__c,Start_Date__c,Milestone__c,Progress_Percentage__c,Role__c,Projects__c ,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c];      
    }
}
Visualforce Page:
<apex:page controller="onPickSelDisRecords1" tabStyle="Account">
    <apex:form >
    <apex:pageBlock id="xxxpb1" title="TimeSheet Entry">
        <apex:pageBlockSection columns="2"> 
            
            <apex:inputField value="{!tsObj.Projects__c}"/>
            <apex:inputField value="{!tsObj.Task_If_Assigned__c}"/>
            <apex:inputField value="{!tsObj.Start_Date__c}"/>
            <apex:inputField value="{!tsObj.End_Date__c}"/>  
            <apex:inputField value="{!tsObj.Assigned_to__c}"/> 
            <apex:inputField value="{!tsObj.Allocation__c}"/> 
            <apex:inputField value="{!tsObj.Time_Spent_Hrs__c}"/>
            <apex:inputField value="{!tsObj.Billable__c}"/>
            <apex:inputField value="{!tsObj.Progress_Percentage__c}"/>
            <apex:inputField value="{!tsObj.Role__c}"/>
            <apex:inputField value="{!tsObj.Milestone__c}"/>
        </apex:pageBlockSection>
       
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}" reRender="pbTable"/>
            <apex:commandButton value="Cancel" action="{!cancel}" reRender="pbTable"/>
        </apex:pageBlockButtons>
        
    </apex:pageBlock>
    
        <apex:pageBlock >
            <apex:pageBlockSection title="Weeks Selection" collapsible="false">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Week"/>
                    <apex:actionRegion>
                    <apex:selectList size="1" value="{!val}">
                        <apex:selectOption itemLabel="All Time" itemValue="AllTime"/>
                        <apex:selectOption itemLabel="Current Week" itemValue="CurrentWeek"/>
                        <apex:selectOption itemLabel="Last Week" itemValue="LastWeek"/>                     
                        <apex:actionSupport event="onchange" reRender="pbTable"/>
                    </apex:selectList>
                    </apex:actionRegion>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageblockSection title="Records" columns="1" id="pbTable">
                <apex:pageBlockTable value="{!accounts}" var="acc">
                    <apex:column value="{!acc.Name}"/>
                </apex:pageBlockTable>
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.


 

All Answers

V V Satyanarayana MaddipatiV V Satyanarayana Maddipati
<apex:actionSupport event="onchange" action="{!viewData}" reRender="block"/>

make sure the picklsit field is inside the rendering block.
nikita dhamalnikita dhamal
i have chechked it .. its in the block only but still getting null value
Srinivas SSrinivas S
Hi Nikita,

Note: Please replace 'Account' with your custom object api name. onchange just refresh the pageblocksection, viewData method is not required.

Please find the following solution which is tested -
Controller Class:
public with sharing class onPickSelDisRecords {
    public String val {get;set;}
    
    public onPickSelDisRecords() {
        val = 'Current Week';
    }
    
    public List<Account> getAccounts() {
        if(val == 'Current Week')
            return [select id, name from Account limit 1];
        else if(val == 'Last Week')
            return [select id, name from Account limit 2];
        else
            return null;
    }
}

Visualforce Page:
<apex:page controller="onPickSelDisRecords" tabStyle="Account">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection title="Weeks Selection" collapsible="false">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Week"/>
                    <apex:selectList size="1" value="{!val}">
                        <apex:selectOption itemLabel="Current Week" itemValue="Current Week"/>
                        <apex:selectOption itemLabel="Last Week" itemValue="Last Week"/>
                        <apex:actionSupport event="onchange" reRender="pbTable"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageblockSection title="Records" columns="1" id="pbTable">
                <apex:pageBlockTable value="{!accounts}" var="acc">
                    <apex:column value="{!acc.Name}"/>
                </apex:pageBlockTable>
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
nikita dhamalnikita dhamal
@srinivasa
i am sending u my code in which i implemented your logic. when i do it individually it works but after integratio it doesn't.

page:
<apex:page controller="onPickSelDisRecords">
    <apex:form >
    <apex:pageBlock id="xxxpb1" title="TimeSheet Entry">
        <apex:pageBlockSection columns="2"> 
            
            <apex:inputField value="{!tsObj.Projects__c}"/>
            <apex:inputField value="{!tsObj.Task_If_Assigned__c}"/>
            <apex:inputField value="{!tsObj.Start_Date__c}"/>
            <apex:inputField value="{!tsObj.End_Date__c}"/>  
            <apex:inputField value="{!tsObj.Assigned_to__c}"/> 
            <apex:inputField value="{!tsObj.Allocation__c}"/> 
            <apex:inputField value="{!tsObj.Time_Spent_Hrs__c}"/>
            <apex:inputField value="{!tsObj.Billable__c}"/>
            <apex:inputField value="{!tsObj.Progress_Percentage__c}"/>
            <apex:inputField value="{!tsObj.Role__c}"/>
            <apex:inputField value="{!tsObj.Milestone__c}"/>
        </apex:pageBlockSection>
       
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}" reRender="pbTable"/>
            <apex:commandButton value="Cancel" action="{!cancel}" reRender="pbTable"/>
        </apex:pageBlockButtons>
        
    </apex:pageBlock>
    
        <apex:pageBlock >
            <apex:pageBlockSection title="Weeks Selection" collapsible="false">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Week"/>
                    <apex:selectList size="1" value="{!val}">
                        <apex:selectOption itemLabel="All Time" itemValue="AllTime"/>
                        <apex:selectOption itemLabel="Current Week" itemValue="CurrentWeek"/>
                        <apex:selectOption itemLabel="Last Week" itemValue="LastWeek"/>
                     
                        <apex:actionSupport event="onchange" reRender="pbTable"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageblockSection title="Records" columns="1" id="pbTable">
                <apex:pageBlockTable value="{!accounts}" var="acc">
                    <apex:column value="{!acc.Name}"/>
                </apex:pageBlockTable>
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

controller:
public with sharing class onPickSelDisRecords {
public String val {get;set;}
public Time_Log__c tsObj{get;set;}
public boolean checkbx{get;set;}
public boolean flag= false;
public Decimal d;

    public onPickSelDisRecords() {
    system.debug('***************************************'+val);
        val = 'Current Week';
        tsObj = new Time_Log__c();
    }
    
    public PageReference cancel(){
 
    PageReference page = new PageReference('/apex/Demo');
    page.setRedirect(true);
    return page;
 }
    public PageReference save()
{
PageReference page = new PageReference('/apex/Demo');
    List<Time_Log__c> timesheetentries = new List<Time_Log__c>();
    timesheetentries = [select id,name,Allocation__c,Projects__c ,Assigned_to__c,End_Date__c,Action_Status__c,Billable__c,Start_Date__c,Milestone__c,Progress_Percentage__c,Role__c,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c ];
     
    for(Time_Log__c ts: timesheetentries)
    {
        if(ts.Projects__c==tsObj.Projects__c && ts.Task_If_Assigned__c==tsObj.Task_If_Assigned__c)
        {
             d=tsObj.Time_Spent_Hrs__c;
             flag=true;
             }
             }
             if(flag == true)
             {
             tsObj=[ select id,name,Allocation__c,Assigned_to__c,Projects__c ,Action_Status__c,Start_Date__c,End_Date__c,Billable__c,Milestone__c,Progress_Percentage__c,Role__c,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c where Projects__c=:tsObj.Projects__c and Task_If_Assigned__c=:tsObj.Task_If_Assigned__c];
             tsObj.Time_Spent_Hrs__c= tsObj.Time_Spent_Hrs__c+d;
             update tsObj;
             page.setRedirect(true);
         
             }
        else
        {
             insert tsObj;
             page.setRedirect(true);
        }
  
    return page;
 }
    
    public List<Time_Log__c > getAccounts() {
        if(val == 'CurrentWeek')
       return [select id,name,Allocation__c,Assigned_to__c,Action_Status__c,End_Date__c,Billable__c,Start_Date__c,Milestone__c,Progress_Percentage__c,Role__c,Projects__c ,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c where LastModifiedDate = THIS_WEEK];
      
        else if(val == 'LastWeek')
       return [select id,name,Allocation__c,Assigned_to__c,Action_Status__c,End_Date__c,Billable__c,Start_Date__c,Milestone__c,Progress_Percentage__c,Role__c,Projects__c ,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c where LastModifiedDate = LAST_WEEK];
      
        else
      return [select id,name,Allocation__c,Assigned_to__c,Action_Status__c,End_Date__c,Billable__c,Start_Date__c,Milestone__c,Progress_Percentage__c,Role__c,Projects__c ,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c];
   
    }
}
Srinivas SSrinivas S
Hi Nikita,

I guess issue is with the form. If you are not populating the required fields in the form and try to change the 'Week' picklist which is invoking an apex method, it will try to send the entire visualforce page to server to process. When it send since few mandatory fields we are not filling up functionality is not working as expected.

To avoid the above issue, we should wrap the selectlist inside of action region. It will resolve the issue.
I have tested, it is working for me.

Controller Class:
public with sharing class onPickSelDisRecords1 {
    public String val {get;set;}
    public Time_Log__c tsObj{get;set;}
    public boolean checkbx{get;set;}
    public boolean flag= false;
    public Decimal d;

    public onPickSelDisRecords1() {
    system.debug('***************************************'+val);
        val = 'Current Week';
        tsObj = new Time_Log__c();
    }
    
    public PageReference cancel(){   
        PageReference page = new PageReference('/apex/Demo');
        page.setRedirect(true);
        return page;
    }
    public PageReference save() {
        PageReference page = new PageReference('/apex/Demo');
        List<Time_Log__c> timesheetentries = new List<Time_Log__c>();
        timesheetentries = [select id,name,Allocation__c,Projects__c ,Assigned_to__c,End_Date__c,Action_Status__c,Billable__c,Start_Date__c,Milestone__c,Progress_Percentage__c,Role__c,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c ];

        for(Time_Log__c ts: timesheetentries){
            if(ts.Projects__c==tsObj.Projects__c && ts.Task_If_Assigned__c==tsObj.Task_If_Assigned__c) {
                d=tsObj.Time_Spent_Hrs__c;
                flag=true;
            }
        }
        if(flag == true) {
            tsObj=[ select id,name,Allocation__c,Assigned_to__c,Projects__c ,Action_Status__c,Start_Date__c,End_Date__c,Billable__c,Milestone__c,Progress_Percentage__c,Role__c,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c where Projects__c=:tsObj.Projects__c and Task_If_Assigned__c=:tsObj.Task_If_Assigned__c];
            tsObj.Time_Spent_Hrs__c= tsObj.Time_Spent_Hrs__c+d;
            update tsObj;
            page.setRedirect(true);
        }
        else {
            insert tsObj;
            page.setRedirect(true);
        }
        return page;
    }
    
    public List<Time_Log__c > getAccounts() {
        if(val == 'CurrentWeek')
            return [select id,name,Allocation__c,Assigned_to__c,Action_Status__c,End_Date__c,Billable__c,Start_Date__c,Milestone__c,Progress_Percentage__c,Role__c,Projects__c ,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c where LastModifiedDate = THIS_WEEK];
        else if(val == 'LastWeek')
            return [select id,name,Allocation__c,Assigned_to__c,Action_Status__c,End_Date__c,Billable__c,Start_Date__c,Milestone__c,Progress_Percentage__c,Role__c,Projects__c ,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c where LastModifiedDate = LAST_WEEK];
        else
            return [select id,name,Allocation__c,Assigned_to__c,Action_Status__c,End_Date__c,Billable__c,Start_Date__c,Milestone__c,Progress_Percentage__c,Role__c,Projects__c ,Activity_Type__c,Task_If_Assigned__c,Time_Spent_Hrs__c from Time_Log__c];      
    }
}
Visualforce Page:
<apex:page controller="onPickSelDisRecords1" tabStyle="Account">
    <apex:form >
    <apex:pageBlock id="xxxpb1" title="TimeSheet Entry">
        <apex:pageBlockSection columns="2"> 
            
            <apex:inputField value="{!tsObj.Projects__c}"/>
            <apex:inputField value="{!tsObj.Task_If_Assigned__c}"/>
            <apex:inputField value="{!tsObj.Start_Date__c}"/>
            <apex:inputField value="{!tsObj.End_Date__c}"/>  
            <apex:inputField value="{!tsObj.Assigned_to__c}"/> 
            <apex:inputField value="{!tsObj.Allocation__c}"/> 
            <apex:inputField value="{!tsObj.Time_Spent_Hrs__c}"/>
            <apex:inputField value="{!tsObj.Billable__c}"/>
            <apex:inputField value="{!tsObj.Progress_Percentage__c}"/>
            <apex:inputField value="{!tsObj.Role__c}"/>
            <apex:inputField value="{!tsObj.Milestone__c}"/>
        </apex:pageBlockSection>
       
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}" reRender="pbTable"/>
            <apex:commandButton value="Cancel" action="{!cancel}" reRender="pbTable"/>
        </apex:pageBlockButtons>
        
    </apex:pageBlock>
    
        <apex:pageBlock >
            <apex:pageBlockSection title="Weeks Selection" collapsible="false">
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Week"/>
                    <apex:actionRegion>
                    <apex:selectList size="1" value="{!val}">
                        <apex:selectOption itemLabel="All Time" itemValue="AllTime"/>
                        <apex:selectOption itemLabel="Current Week" itemValue="CurrentWeek"/>
                        <apex:selectOption itemLabel="Last Week" itemValue="LastWeek"/>                     
                        <apex:actionSupport event="onchange" reRender="pbTable"/>
                    </apex:selectList>
                    </apex:actionRegion>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageblockSection title="Records" columns="1" id="pbTable">
                <apex:pageBlockTable value="{!accounts}" var="acc">
                    <apex:column value="{!acc.Name}"/>
                </apex:pageBlockTable>
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.


 
This was selected as the best answer
Srinivas SSrinivas S
Pleaase remove </b> and <b> from the above vf page otherwise you will end up with error.

------------
Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
nikita dhamalnikita dhamal
Thanks Srinivasa its working now!!