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
Code+1Code+1 

pass inputfield value from visualforce page to apex controller

Hello All,

 I am trying to pass the Input field value from Visualforce page to apex controller class.
 Please let me know how to capture the value.

 Below is the code.

<apex:page standardController="Activity_Tracker__c" extensions="dependentPicklistController"> 
  <apex:messages style="color:red"></apex:messages>
    <apex:form >
        <apex:pageblock >
            <apex:pageblockSection >
               <apex:inputField value="{!Activity_Tracker__c.Category__c}" />
               <apex:inputField value="{!Activity_Tracker__c.Sub_Customers__c}" />
            </apex:pageblockSection>
        </apex:pageblock>
        

         <apex:commandButton action="{!search}" value="search"/>
         <apex:pageBlock >
         <apex:pageBlockTable value="{!records}" var="item">
                <apex:column value="{!item.Category__c}"/>
                <apex:column value="{!item.Sub_Customers__c}"/>
                <apex:column value="{!item.Status__c }"/>
           </apex:pageBlockTable>
         </apex:pageBlock>
    </apex:form>
</apex:page>


Controller --
public with sharing class dependentPicklistController{
    public List<Activity_Tracker__c> records{get;set;}
    public Activity_Tracker__c  Category__c{get; set;}
 
    public pagereference search()
    {
       records=[Select ID, Category__c, Sub_Customers__c, Status__c From Activity_Tracker__c   ];
       system.debug('11111' +Query);
       return null;
    }
}

In this I am trying to capture Category__c, Sub_Customers__c (Dependent Picklists) in the Apex Class.

Thanks... 
Best Answer chosen by Code+1
Donald BlayDonald Blay
It looks like you're tyring to bind your input fields to the SObject definition and not an instance of it.  I think the Value attributes of the InputField should be the instance variable in your controller, the one you named "Category__C".  Then in your controller you can just reference that variable and (Category__c) it will have the values that were selected on the page.  
Though i would recomend renaming that variable, having it with the same name as a custome field could be confusing.  

Controler: 
public Activity_Tracker__c  tracker{get; set;}

VF page:
               <apex:inputField value="{!tracker.Category__c}" />
               <apex:inputField value="{!tracker{.Sub_Customers__c}" />

All Answers

Donald BlayDonald Blay
It looks like you're tyring to bind your input fields to the SObject definition and not an instance of it.  I think the Value attributes of the InputField should be the instance variable in your controller, the one you named "Category__C".  Then in your controller you can just reference that variable and (Category__c) it will have the values that were selected on the page.  
Though i would recomend renaming that variable, having it with the same name as a custome field could be confusing.  

Controler: 
public Activity_Tracker__c  tracker{get; set;}

VF page:
               <apex:inputField value="{!tracker.Category__c}" />
               <apex:inputField value="{!tracker{.Sub_Customers__c}" />
This was selected as the best answer
ManojjenaManojjena

Hi Krishna,

As your page has standrad controller you should have one  argument constructor . From the standradController you can get the record and related field value .
Check links it will help you .
https://www.salesforce.com/docs/developer/pages/Content/apex_pages_standardcontroller.htm
Please let me know if you need any further help .
Code+1Code+1
Hi Donald,

 Yes, The functionality worked fine as expected .Thanks..

Thanks Manoj for Sharing..
 
Somnath Paul 3Somnath Paul 3
Hi ,

I was using this very same approach where I instantiated by object in the controller and tried to reference the same using !testObj.Object_Name__c, where Object_Name__c is my custom picklist field.

However, on selection of a value 'Account' from that picklist field, my rerender request should take me to a different pageblocksection. Someway that does not seem to be working.

Can you tell me where I am going wrong. Without doing a instantion in the controller, the re-renedered functionality is working fine. (When I am using TestA__c.Object_Name__c=='Account')


Below is my code:

<apex:inputField value="{!testObj.Object_Name__c}">
                    <apex:actionSupport event="onchange" reRender="ajaxrequest" />
                    </apex:inputfield>
      <!--   </apex:actionRegion>-->
                 </apex:PageBlockSectionItem>
            </apex:PageBlockSection>
         
         <apex:outputPanel id="ajaxrequest">  
                
                    <apex:pageBlockSection rendered="{!testObj.Object_Name__c=='Account'}">
                       <apex:pageBlock title="Enter Account Details" id="AccountInsert">
                               <apex:pageblockSection >


Opened up an old thread. Any help would be a huge help.