+ Start a Discussion
Uttpal chandraUttpal chandra 

How to insert data using custom controller?

Hi all,
I have created one vf page and one custom controller.But whenever i am trying to save data it is not happening and also it is not showing any error.

Anyone help me on this.



Controller
public class EmployeeController
{
     
    
    public Employee__c abc{get;set;}
  
    

    public EmployeeController()
    {     
       abc = new Employee__c();

       
    //   abc = [Select id,Name,Birth_Date__c from Employee__c];
       
    }
    
  
    
    public PageReference save(){
        
        insert abc;
        return null;
        
    }
    
       
}



Vfpage
<apex:page lightningStylesheets="true" controller="EmployeeController" showHeader="false" standardStylesheets="false">


 <apex:pageBlock >    
        <apex:pageBlockSection columns="2">
             <apex:inputField value="{!abc.Name}"/> 
             <apex:inputField value="{!abc.Custom_Code__c}"/>      
        </apex:pageBlockSection>
            
        
               
            <apex:pageBlockSection title="Work Address">
                 <apex:inputField value="{!abc.Work_Address__c}"/>
                 <apex:inputField value="{!abc.Work_Address_Line_2__c}"/>
                 <apex:inputField value="{!abc.Work_City__c}"/>

                <apex:commandButton value="Save" action="{!save}" styleClass="slds-button slds-button_brand"/>
                
             </apex:pageBlockSection>
       </apex:pageBlock>

Thanks in advance
Best Answer chosen by Uttpal chandra
Khan AnasKhan Anas (Salesforce Developers) 
Hi Uttpal,

Greetings to you!

You need to use <apex:form> tag. It is a section of a Visualforce page that allows users to enter input and then submit it with an <apex:commandButton> or <apex:commandLink>.

Also, I suggest to use <apex:pageMessages> tag. This component displays all messages that were generated for all components on the current page, presented using the Salesforce styling. And use try-catch in the controller. Apex uses try, catch and finally statements to handle exceptions.

Change your code as below.

Visualforce:
<apex:page lightningStylesheets="true" controller="EmployeeController" showHeader="false" standardStylesheets="false">
    
    <apex:form>
        <apex:pageMessages/>
        <apex:pageBlock >    
            <apex:pageBlockSection columns="2">
                <apex:inputField value="{!abc.Name}"/> 
                <apex:inputField value="{!abc.Custom_Code__c}"/>      
            </apex:pageBlockSection>
            
            <apex:pageBlockSection title="Work Address">
                <apex:inputField value="{!abc.Work_Address__c }"/>
                <apex:inputField value="{!abc.Work_Address_Line_2__c }"/>
                <apex:inputField value="{!abc.Work_City__c }"/>
                
                
                
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!save}" styleClass="slds-button slds-button_brand"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class EmployeeController {
    
    public Employee__c abc {get;set;}
    
    public EmployeeController() {     
        abc = new Employee__c();
    }
    
    public PageReference save(){
        try{
        	INSERT abc;
        }
        catch (exception e){
            
        }
        return null;
    }
}

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 Uttpal,

Greetings to you!

You need to use <apex:form> tag. It is a section of a Visualforce page that allows users to enter input and then submit it with an <apex:commandButton> or <apex:commandLink>.

Also, I suggest to use <apex:pageMessages> tag. This component displays all messages that were generated for all components on the current page, presented using the Salesforce styling. And use try-catch in the controller. Apex uses try, catch and finally statements to handle exceptions.

Change your code as below.

Visualforce:
<apex:page lightningStylesheets="true" controller="EmployeeController" showHeader="false" standardStylesheets="false">
    
    <apex:form>
        <apex:pageMessages/>
        <apex:pageBlock >    
            <apex:pageBlockSection columns="2">
                <apex:inputField value="{!abc.Name}"/> 
                <apex:inputField value="{!abc.Custom_Code__c}"/>      
            </apex:pageBlockSection>
            
            <apex:pageBlockSection title="Work Address">
                <apex:inputField value="{!abc.Work_Address__c }"/>
                <apex:inputField value="{!abc.Work_Address_Line_2__c }"/>
                <apex:inputField value="{!abc.Work_City__c }"/>
                
                
                
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!save}" styleClass="slds-button slds-button_brand"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class EmployeeController {
    
    public Employee__c abc {get;set;}
    
    public EmployeeController() {     
        abc = new Employee__c();
    }
    
    public PageReference save(){
        try{
        	INSERT abc;
        }
        catch (exception e){
            
        }
        return null;
    }
}

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
Uttpal chandraUttpal chandra
Hi Anas,

Thanks for the help  i didn't know about  "<apex:pageMessages/>" tag it is very useful for me from that i got my error.

Regards,
Uttpal
Mahesh TandanMahesh Tandan

thank you so much khan Anas

your answer really help me a lot