[SalesForce] apex:selectList value is always returing null to controller

im trying get the selected recordtype into the controller to build additional rendering logic in showTranLog() method based on input selection but its always returning null. when i insert the record in saveproceed() method its getting set fine.

<apex:page standardController="Contact" extensions="TaskExtensionCtrl">

  <apex:form >
    <apex:pageBlock title="New Log" mode="edit" id='pb'>
    <apex:pageBlockSection id="selectedRecordType" columns="1">
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="Choose Record Type" for="rt" />         
            <apex:panelGrid columns="2">
            <apex:selectList value="{!selectedrecType}" multiselect="false"  size="1" required="true">
                <apex:selectOptions value="{!rectypes}"/>
                <apex:actionSupport event="onchange" action="{!showTranLog}" reRender="pb" status="status" immediate="true"/>
            </apex:selectList> 
            </apex:panelGrid>       
        </apex:pageBlockSectionItem>
    </apex:pageBlockSection>
     <apex:pageBlockButtons location="bottom">
        <apex:commandButton value="Save" action="{!saveAndProceed}" />
      </apex:pageBlockButtons>
    </apex:pageBlock>
  </apex:form>
</apex:page>

Controller:

public with sharing class TaskExtensionCtrl {
    public String partId;
    public Task task {get;set;}
    public boolean isShowTranLog  {get;set;}{isShowTranLog=false;}
    public string selectedrecType{get;set;}

    public TaskExtensionCtrl(ApexPages.StandardController controller){
        partId=ApexPages.currentpage().getParameters().get('id');
        createTaskRecord();
    }

    public void createTaskRecord() {
        system.debug(LoggingLevel.error,'in creat task'+selectedrecType);
        task = new Task();
    }
    public PageReference showTranLog(){
          this.isShowTranLog = true;
          system.debug(LoggingLevel.error,'task record type in showTranLog#'+ task.RecordTypeID+Task);
          return null;
     }    
    public List<SelectOption> getrectypes() {

        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('','--Select Record Type --'));
        for(RecordType rt:[select id,name from RecordType where sobjecttype='task']){
            options.add(new SelectOption(rt.id,rt.name));    
        }
        return options;
    }

    public String getselectedrecType() {
        return selectedrecType;
    }

    public void setselectedrecType(String selectedrecType) {
        this.selectedrecType= selectedrecType;
    }

    public PageReference saveAndProceed() {

        try {
            task.RecordTypeID=selectedrecType;
        insert task;
        system.debug(LoggingLevel.error,'In Save#'+ task+task.RecordTypeID+task.RecordType);
        } 
        catch(DMLException e) {
        task.addError(e.getMessage());
        return null;
        }

        PageReference pf = new PageReference('/' + task.Id + '?retURL=/' + partId);
        pf.setRedirect(true);
        return pf;
    }
}

Best Answer

you already have the selected recordtype getter and setter at the top of your class:

public string selectedrecType{get;set;}

just remove this additional getter and setter:

public String getselectedrecType() {
    return selectedrecType;
}

public void setselectedrecType(String selectedrecType) {
    this.selectedrecType= selectedrecType;
}

and remove the immediate="true" (or set it to false) from ActionSupport

setting immediate to true when calling controller action will execute the action in the backend, but it won't carry the form data with it. so you need to set it to false

Related Topic