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
Satish PrajapatSatish Prajapat 

How to get imput value from <apex:inputText> tag in Apex class

Hello Experts,
  • I have one apex class and I displaying the list of fileds from FieldSet on VF page.
  • There are some input fields, which i want in my apex code.
    <!-- VF Page Snippet-->
    *******
    *******
    <apex:repeat value="{!fields}" var="f">
          <apex:outputText value="{!f.Label}"/>
          <apex:inputText WHAT_LOGIC_I_SHOULD_USE_HERE />  <br/>
    </apex:repeat>
    *******
    *******
    
    /* Apex Code Snippet  */
    
    public List<Schema.FieldSetMember> getFields() 
    {
          return SObjectType.Contact.FieldSets.Quiz_Field_Set.getFields();
    }
  • Whatever values I put into Input Text Box, that all value I want into my Apex Code.
Satish PrajapatSatish Prajapat
Hello LBK,
  • When I am using thefollowing code:
    public string txtValue {get; set;}
    Then its printing only last value.
  • I am giving input in this manner:
  • User-added imageT
  • Then its printing only Business Fax Number.
VivekShindeVivekShinde
You need to declare a get, set for Contact object in the controller as follows:
public class VFTestController {
    public Contact objContact {get; set;}
}
Then you need to modify code of your visualforce page as follows:
<apex:repeat value="{!fields}" var="f">
      <apex:outputText value="{!f.Label}"/>
      <apex:inputField value="{!objContact[f.fieldPath]}" />  <br/>
</apex:repeat>
You can then debug the objContact variable in the controller to get the values of the fields set using the visualforce page.

For more details on working with the fieldsets, go through this documentation on fieldsets: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_dynamic_vf_field_sets.htm

Thanks,
Vivek Shinde
Satish PrajapatSatish Prajapat
Hello Vivek,
  • I have the problem, we can access field value from VF page with the help of wrapper class.
  • //VF Page
    <apex:repeat value="{!list_FSInput}" var="obj1" >
              <apex:outputText value="{!obj1.fieldName}" />
              <apex:inputText value="{!obj1.fieldValue}"  />  <br/>
    </apex:repeat>
    
    //Apex class A
    public List<FieldSetInput> list_FSInput {get;set;}
    //write it below line in constructor
    list_FSInput = new List<FieldSetInput>();
    
    for(Schema.FieldSetMember ss: SObjectType.Contact.FieldSets.Quiz_Field_Set.getFields())
    {
             list_FSInput.add(new FieldSetInput(ss.FieldPath));
    }
    
    //Wrapper Class (inner class of A)
    public with sharing class FieldSetInput
    {
            public String fieldName {get;set;}
            public String fieldValue {get;set;}
            public FieldSetInput(String f1)
            {
                   fieldName = f1;
            }
    }
  • list_FSInput list contain the API name of the field and its value.
VivekShindeVivekShinde
Although your approach is correct. You will only be able to bind fields of text data type. You will not be able to bind fields of different data types such as picklist, date, datetime, boolean, etc. Keeping these things into consideration I had posted my solution earlier.
Satish PrajapatSatish Prajapat

Hello Vivek,

I agree with your suggestion but when I tried your logic then I can not able to get the inputText value into My apex code.
It may possible that i am doing some mistake, And thanks for your reply.

VivekShindeVivekShinde
Hi Satish,

You need to use inputField instead of inputText and then let's say you need to access the value of First Name, for this you need to use objContact.FirstName. Similar approach needs to be followed fo the other fields.

Thanks