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
Shivendra Singh PawarShivendra Singh Pawar 

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.

We  have a custom Object and fields,
What I want is to fill the form and insert a record to the Student Object.

Someone, please help me..!!


Below is the Code:

Visualforce page :

<apex:pageBlockButtons > 
                <apex:commandButton action="{!insertField}" value="Save"/> 
            </apex:pageBlockButtons> 
            <apex:pageBlockSection title="Crud Operation Using Apex and Visualforce" columns="2" collapsible="true"> 
                <apex:inputField value="{!stud.Name}"/>
                <apex:inputField value="{!stud.shivendra__Phone__c}"/>
                <apex:inputField value="{!stud.shivendra__Email__c}"/>
                <apex:inputField value="{!stud.shivendra__Age__c}"/>
                <apex:inputField value="{!stud.shivendra__State__c}"/>
                <apex:inputField value="{!stud.shivendra__City__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock> 


Apex Class Code:

public class crud1 {
    
    // Instance to use Fields API in a a VF Page
    public shivendra__Student__c stud {get; set;}
    
    public String Name {get; set;}
    public String Email {get; set;}
    public String Age {get; set;}
    public String Phone {get; set;}
    public String State {get; set;}
    public String City {get; set;}
    
    //Function used to Insert a Record
    public PageReference insertField(){
        shivendra__Student__c studentRecord = new shivendra__Student__c();
        
        studentRecord.name = Name;
        studentRecord.shivendra__Phone__c = stud.shivendra__Phone__c;    
        studentRecord.shivendra__Email__c = Email;
        studentRecord.shivendra__Age__c = Age;
        studentRecord.shivendra__State__c = State;
        studentRecord.shivendra__City__c = City;
        
        insert studentRecord;        
        pagereference ref = new pagereference('/apex/CrudOperations1');
        ref.setredirect(true);
        return ref;        
    }   
  
}
Best Answer chosen by Shivendra Singh Pawar
Khan AnasKhan Anas (Salesforce Developers) 
Hi Shivendra,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="Crud1">
   <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons > 
                 <apex:commandButton action="{!insertField}" value="Save"/> 
            </apex:pageBlockButtons> 
            <apex:pageBlockSection title="Crud Operation Using Apex and Visualforce" columns="2" collapsible="true"> 
                <apex:inputField value="{!stud.Name}"/>
                <apex:inputField value="{!stud.shivendra__Phone__c}"/>
                <apex:inputField value="{!stud.shivendra__Email__c}"/>
                <apex:inputField value="{!stud.shivendra__Age__c}"/>
                <apex:inputField value="{!stud.shivendra__State__c}"/>
                <apex:inputField value="{!stud.shivendra__City__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock> 
   </apex:form>
</apex:page>

Controller:
public class Crud1 {
    
    // Instance to use Fields API in a a VF Page
    public shivendra__Student__c stud {get; set;}
    
    //Constructor
    public Crud1(){
        stud = new shivendra__Student__c();
    }

    //Function used to Insert a Record
    public PageReference insertField(){
        try {  
            INSERT stud;
        }
        catch (Exception e) {
            ApexPages.addMessages (e);
            return null;
        }
        PageReference ref = new PageReference('/' + stud.Id);
        return ref;      
    }   
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Shivendra,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="Crud1">
   <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons > 
                 <apex:commandButton action="{!insertField}" value="Save"/> 
            </apex:pageBlockButtons> 
            <apex:pageBlockSection title="Crud Operation Using Apex and Visualforce" columns="2" collapsible="true"> 
                <apex:inputField value="{!stud.Name}"/>
                <apex:inputField value="{!stud.shivendra__Phone__c}"/>
                <apex:inputField value="{!stud.shivendra__Email__c}"/>
                <apex:inputField value="{!stud.shivendra__Age__c}"/>
                <apex:inputField value="{!stud.shivendra__State__c}"/>
                <apex:inputField value="{!stud.shivendra__City__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock> 
   </apex:form>
</apex:page>

Controller:
public class Crud1 {
    
    // Instance to use Fields API in a a VF Page
    public shivendra__Student__c stud {get; set;}
    
    //Constructor
    public Crud1(){
        stud = new shivendra__Student__c();
    }

    //Function used to Insert a Record
    public PageReference insertField(){
        try {  
            INSERT stud;
        }
        catch (Exception e) {
            ApexPages.addMessages (e);
            return null;
        }
        PageReference ref = new PageReference('/' + stud.Id);
        return ref;      
    }   
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Shivendra Singh PawarShivendra Singh Pawar
Hi Khan Anas,

Thanks for the code.
It works fine.


Thanks
Shivendra Singh Pawar